Working With time Series - date_range() Method in Python Pandas
*It is used to generate a range of date values.
*pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
The Sample Code
With start and end parameters
# Generate 8 Dates
Input - pd.date_range(start='1/1/2019', end='1/08/2019')
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08'], dtype='datetime64[ns]', freq='D')
Input - pd.date_range(start='1/1/2019', periods=8)
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08'], dtype='datetime64[ns]', freq='D')
Input - pd.date_range(end='1/1/2019', periods=8)
DatetimeIndex(['2018-12-25', '2018-12-26', '2018-12-27', '2018-12-28', '2018-12-29', '2018-12-30', '2018-12-31', '2019-01-01'], dtype='datetime64[ns]', freq='D')
Input - pd.date_range(start='1/1/2019', end='1/20/2019', periods=10)
DatetimeIndex(['2019-01-01 00:00:00', '2019-01-03 02:40:00', '2019-01-05 05:20:00', '2019-01-07 08:00:00', '2019-01-09 10:40:00', '2019-01-11 13:20:00', '2019-01-13 16:00:00', '2019-01-15 18:40:00', '2019-01-17 21:20:00', '2019-01-20 00:00:00'], dtype='datetime64[ns]', freq=None)
Input - pd.date_range(start='1/1/2019', periods=12, freq='B')
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10', '2019-01-11', '2019-01-14', '2019-01-15', '2019-01-16'], dtype='datetime64[ns]', freq='B')
# How to set timezone
Input - pd.date_range(start='1/1/2019', periods=22, tz='Asia/Kolkata')
DatetimeIndex(['2019-01-01 00:00:00+05:30', '2019-01-02 00:00:00+05:30', '2019-01-03 00:00:00+05:30', '2019-01-04 00:00:00+05:30', '2019-01-05 00:00:00+05:30', '2019-01-06 00:00:00+05:30', '2019-01-07 00:00:00+05:30', '2019-01-08 00:00:00+05:30', '2019-01-09 00:00:00+05:30', '2019-01-10 00:00:00+05:30', '2019-01-11 00:00:00+05:30', '2019-01-12 00:00:00+05:30', '2019-01-13 00:00:00+05:30', '2019-01-14 00:00:00+05:30', '2019-01-15 00:00:00+05:30', '2019-01-16 00:00:00+05:30', '2019-01-17 00:00:00+05:30', '2019-01-18 00:00:00+05:30', '2019-01-19 00:00:00+05:30', '2019-01-20 00:00:00+05:30', '2019-01-21 00:00:00+05:30', '2019-01-22 00:00:00+05:30'], dtype='datetime64[ns, Asia/Kolkata]', freq='D')
dates = pd.date_range(start='1/1/2020', end='1/10/2020')
dates
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10'], dtype='datetime64[ns]', freq='D')
df.set_index(dates, inplace=True)
df
meantemp | humidity | wind_speed | meanpressure | |
---|---|---|---|---|
2020-01-01 | 15.913043 | 85.869565 | 2.743478 | 59.000000 |
2020-01-02 | 18.500000 | 77.222222 | 2.894444 | 1018.277778 |
2020-01-03 | 17.111111 | 81.888889 | 4.016667 | 1018.333333 |
2020-01-04 | 18.700000 | 70.050000 | 4.545000 | 1015.700000 |
2020-01-05 | 18.388889 | 74.944444 | 3.300000 | 1014.333333 |
2020-01-06 | 19.318182 | 79.318182 | 8.681818 | 1011.772727 |
2020-01-07 | 14.708333 | 95.833333 | 10.041667 | 1011.375000 |
2020-01-08 | 15.684211 | 83.526316 | 1.950000 | 1015.550000 |
2020-01-09 | 14.571429 | 80.809524 | 6.542857 | 1015.952381 |