📅  最后修改于: 2023-12-03 14:48:35.616000             🧑  作者: Mango
pd.date_range
- Pythonpd.date_range
is a powerful function provided by the Python pandas library for generating a fixed frequency date range. It is commonly used for creating time-series data or working with temporal data in pandas.
The basic syntax of pd.date_range
is as follows:
pd.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None)
start
(optional): The starting date. Defaults to None.end
(optional): The ending date. Defaults to None.periods
(optional): The number of periods to generate. Either end
or periods
must be specified, but not both.freq
(optional): The frequency of the date range. This can be a frequency string like 'D' for daily, 'M' for monthly, 'A' for annual, etc. Defaults to None.tz
(optional): Time zone name for the date range. Defaults to None.normalize
(optional): If True, normalize start/end dates to midnight. Defaults to False.name
(optional): Name of the resulting DatetimeIndex
. Defaults to None.closed
(optional): Make the interval closed with respect to the given frequency to the end
point. Defaults to None.pd.date_range
returns a DatetimeIndex
that represents the generated date range.
import pandas as pd
# Generate a range of dates from '2022-01-01' to '2022-01-10'
date_range = pd.date_range(start='2022-01-01', end='2022-01-10')
import pandas as pd
# Generate a range of dates with a monthly frequency from '2022-01-01' to '2022-12-31'
date_range = pd.date_range(start='2022-01-01', end='2022-12-31', freq='M')
import pandas as pd
# Generate a range of dates for 5 periods starting from '2022-01-01'
date_range = pd.date_range(start='2022-01-01', periods=5)
import pandas as pd
import pytz
# Generate a range of dates from '2022-01-01' to '2022-01-10' in 'Asia/Kolkata' time zone
date_range = pd.date_range(start='2022-01-01', end='2022-01-10', tz=pytz.timezone('Asia/Kolkata'))
These are just a few examples of how pd.date_range
can be used. It provides great flexibility in generating date ranges according to different requirements.
To summarize, pd.date_range
is a useful function in Python pandas for generating fixed frequency date ranges. It is a convenient tool for working with time-series data and handling temporal operations.