Answers for "pandas select date range"

2

panda get rows with date range

#greater than the start date and smaller than the end date
mask = (df['date'] > start_date) & (df['date'] <= end_date)
df = df.loc[mask]
Posted by: Guest on November-30-2020
1

pandas date_range

index = pd.date_range(start='2020-8-15', end = '2021-8-11', freq='M')
print(index)

>>>DatetimeIndex(['2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30',
                 '2020-12-31', '2021-01-31', '2021-02-28', '2021-03-31',
                 '2021-04-30', '2021-05-31', '2021-06-30', '2021-07-31'],
                 dtype='datetime64[ns]', freq='M')
Posted by: Guest on August-13-2021
1

how to slice dataframe based on daterange in pandas

In [15]: df = pd.DataFrame([1, 2, 3], index=[dt.datetime(2013, 1, 1), dt.datetime(2013, 1, 3), dt.datetime(2013, 1, 5)])

In [16]: df
Out[16]: 
            0
2013-01-01  1
2013-01-03  2
2013-01-05  3

In [22]: start = df.index.searchsorted(dt.datetime(2013, 1, 2))

In [23]: end = df.index.searchsorted(dt.datetime(2013, 1, 4))

In [24]: df.iloc[start:end]
Out[24]: 
            0
2013-01-03  2
Posted by: Guest on June-18-2020

Python Answers by Framework

Browse Popular Code Answers by Language