How to select DateTime within a range in Python?
0

How to select DateTime within a range in Python?

This recipe helps you select DateTime within a range in Python
In [1]:
## How to select DateTime within a range in Python 
def Kickstarter_Example_48():
    print()
    print(format('How to select DateTime within a range in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load library
    import pandas as pd
    # Create data frame
    df = pd.DataFrame()
    # Create datetimes
    df['date'] = pd.date_range('15/12/1999', periods=100000, freq='H')
    # Set index
    df = df.set_index(df['date'])
    # Select observations between two datetimes
    print(); print(df.loc['2002-1-1 01:00:00':'2002-1-1 07:00:00'])
Kickstarter_Example_48()
*****************How to select DateTime within a range in Python******************

                                   date
date
2002-01-01 01:00:00 2002-01-01 01:00:00
2002-01-01 02:00:00 2002-01-01 02:00:00
2002-01-01 03:00:00 2002-01-01 03:00:00
2002-01-01 04:00:00 2002-01-01 04:00:00
2002-01-01 05:00:00 2002-01-01 05:00:00
2002-01-01 06:00:00 2002-01-01 06:00:00
2002-01-01 07:00:00 2002-01-01 07:00:00