Pandas Pd Date Range
[ Pandas Common Functions](#)\\n\\n* * *\\n\\n`pd.date_range()` is a function in the Pandas library used for **generating date ranges**. It can create a DatetimeIndex containing continuous dates, commonly used for creating, indexing, and aligning time series data.\\n\\nIn time series analysis, it is often necessary to generate date sequences at fixed frequencies, such as daily, monthly, or yearly data. `pd.date_range()` provides flexible parameters to meet various frequency requirements.\\n\\n**Word Meaning**: `date_range` means "date range", i.e., generating a set of continuous dates.\\n\\n* * *\\n\\n## Basic Syntax and Parameters\\n\\n`pd.date_range()` is a top-level function in the Pandas library, used to generate date time indexes at specified frequencies.\\n\\n### Syntax Format\\n\\npd.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None)\\n### Parameter Description\\n\\n| Parameter | Type | Required | Description | Default Value |\\n| --- | --- | --- | --- | --- |\\n| start | string or datetime | Optional | Start date. | None |\\n| end | string or datetime | Optional | End date. | None |\\n| periods | integer | Optional | Number of dates to generate. Choose two out of start/end/periods. | None |\\n| freq | string or DateOffset | Optional | Frequency: 'D' (day), 'H' (hour), 'M' (month end), 'MS' (month start), 'Y' (year end), 'W' (week), etc. | 'D' |\\n| tz | string | Optional | Time zone name, such as 'UTC', 'Asia/Shanghai'. | None |\\n| normalize | boolean | Optional | True normalizes time to midnight. | False |\\n| name | string | Optional | Name of the generated DatetimeIndex. | None |\\n| closed | string | Optional | Interval closure: 'left', 'right', None (default, both ends closed). | None |\\n\\n### Return Value Description\\n\\n* **Return Value**: Returns a DatetimeIndex object containing continuous date times.\\n* **Effect**: Generates a date sequence based on the specified start date, end date, and frequency.\\n\\n* * *\\n\\n## Examples\\n\\nLet's thoroughly master the usage of `pd.date_range()` through a series of examples from simple to complex.\\n\\n### Example 1: Basic Usage - Generating Date Ranges\\n\\n## Example\\n\\nimport pandas as pd\\n\\n# 1. Generate date range using start and end parameters (default frequency is daily)\\n\\nprint("=== From 2023-01-01 to 2023-01-10 ===")\\n\\n dates = pd.date_range(start='2023-01-01', end='2023-01-10')\\n\\nprint(dates)\\n\\nprint(f"Type: {type(dates)}")\\n\\nprint(f"Dtype: {dates.dtype}")\\n\\n# 2. Use periods parameter to specify the number of dates to generate\\n\\nprint("n=== Generate 7 days of dates ===")\\n\\n week_dates = pd.date_range(start='2023-01-01', periods=7)\\n\\nprint(week_dates)\\n\\n# 3. Use freq parameter to specify frequency\\n\\nprint("n=== One date per week (freq='W') ===")\\n\\n weekly_dates = pd.date_range(start='2023-01-01', periods=5, freq='W')\\n\\nprint(weekly_dates)\\n\\n# 4. Dates at the beginning of each month (freq='MS' = Month Start)\\n\\nprint("n=== Beginning of each month (freq='MS') ===")\\n\\n monthly_dates = pd.date_range(start='2023-01-01', periods=6, freq='MS')\\n\\nprint(monthly_dates)\\n\\n# 5. Dates at the end of each year (freq='YE' = Year End)\\n\\nprint("n=== End of each year (freq='YE') ===")\\n\\n yearly_dates = pd.date_range(start='2023-01-01', periods=3, freq='YE')\\n\\nprint(yearly_dates)\\n\\n**Run Results:**\\n\\n=== From 2023-01-01 to 2023-01-10 ===DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06', '2023-01-07', '2023-01-08', '2023-01-09', '2023-01-10'], dtype='datetime64', freq='D')Type: <class 'pandas.core.indexes.datetimes.DatetimeIndex'>Data Type: datetime64=== Generate 7 days of dates ===DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06', '2023-01-07'], dtype='datetime64', freq='D')=== One date per week (freq='W') ===DatetimeIndex(['2023-01-01', '2023-01-08', '2023-01-15', '2023-01-22', '2023-01-29'], dtype='datetime64', freq='W-SUN')=== Beginning of each month (freq='MS') ===DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01', '2023-05-01', '2023-06-01'], dtype='datetime64', freq='MS')=== End of each year (freq='YE') ===DatetimeIndex(['2023-12-31', '2024-12-31', '2025-12-31'], dtype='datetime64', freq='YE-DEC')\\n**Code Analysis:**\\n\\n1. `pd.date_range()` generates daily frequency date sequences by default.\\n2. The `freq` parameter supports various frequency abbreviations: 'D' (day), 'W' (week), 'M' (month end), 'MS' (month start), 'Y' (year end), 'H' (hour), etc.\\n3. Returns a DatetimeIndex object, which can be directly used as an index for Series or DataFrame.\\n\\n### Example 2: Generating Time Series Data\\n\\nCreate a DataFrame with a date index for time series analysis.\\n\\n## Example\\n\\nimport pandas as pd\\n\\nimport numpy as np\\n\\n# 1. Create a DataFrame with date index\\n\\nprint("=== Create a simple time series ===")\\n\\n# Generate 30 days of dates\\n\\n date_index = pd.date_range(start='2023-01-01', periods=30, freq='D')\\n\\n# Create DataFrame\\n\\n df = pd.DataFrame({\\n\\n'date': date_index,\\n\\n'value': np.random.randint(10,100, size=30),# Random values\\n\\n'category': ['A','B'] * 15# Alternating categories\\n\\n})\\n\\nprint(df.head(10))\\n\\n# 2. Set date as index\\n\\nprint("n=== Set date as index ===")\\n\\n df_indexed = df.set_index('date')\\n\\nprint(df_indexed.head())\\n\\n# 3. Generate hourly data (for log analysis)\\n\\nprint("n=== Hourly time series ===")\\n\\n hourly_index = pd.date_range(start='2023-01-01 08:00', periods=24, freq='H')\\n\\n hourly_data = pd.DataFrame({\\n\\n'timestamp': hourly_index,\\n\\n'visitors': np.random.randint(0,100, size=24)\\n\\n})\\n\\nprint(hourly_data)\\n\\n**Run Results:**\\n\\n=== Create a simple time series === date value category 0 2023-01-01 67 A 1 2023-01-02 23 B 2 2023-01-03 45 A 3 2023-01-04 89 B 4 2023-01-05 12 A 5 2023-01-06 34 B 6 2023-01-07 56 A 7 2023-01-08 78 B 8 2023-01-09 91 A 9 2023-01-10 55 B === Set dates as index === value category date 2023-01-01 67 A 2023-01-02 23 B 2023-01-03 45 A 2023-01-04 89 B 2023-01-05 12 A 2023-01-06 34 B === Hourly time series === timestamp visitors 0 2023-01-01 08:00:00 251 2023-01-01 09:00:00 412 2023-01-01 10:00:00 773 2023-01-01 11:00:00 124 2023-01-01 12:00:00 585 2023-01-01 13:00:00 336 2023-01-01 14:00:00 457 2023-01-01 15:00:00 678 2023-01-01 16:00:00 299 2023-01-01 17:00:00 5110 2023-01-01 18:00:00 4411 2023-01-01 19:00:00 3612 2023-01-01 20:00:00 2813 2023-01-01 21:00:00 6214 2023-01-01 22:00:00 1915 2023-01-01 23:00:00 7316 2023-01-02 00:00:00 4117 2023-01-02 01:00:00 5518 2023-01-02 02:00:00 3819 2023-01-02 03:00:00 2720 2023-01-02 04:00:00 4921 2023-01-02 05:00:00 6322 2023-01-02 06:00:00 3423 2023-01-02 07:00:00 56\\n**Code Analysis:**\\n\\n* DatetimeIndex can be directly used as an index for DataFrame.\\n* DataFrame with date index supports rich time series operations, such as resampling, rolling calculations, etc.\\n* `freq='H'` can generate hourly-level data, suitable for scenarios like log analysis.\\n\\n### Example 3: Time Zone and closed Parameters\\n\\n## Example\\n\\nimport pandas as pd\\n\\n# 1. Use tz parameter to specify time zone\\n\\nprint("=== Date range with time zone ===")\\n\\n dates_tz = pd.date_range(start='2023-01-01', periods=3, freq='D', tz='Asia/Shanghai')\\n\\nprint(dates_tz)\\n\\nprint(f"Time zone: {dates_tz.tz}")\\n\\n# 2. Use UTC time zone\\n\\nprint("n=== UTC time zone ===")\\n\\n dates_utc = pd.date_range(start='2023-01-01', periods=3, freq='D', tz='UTC')\\n\\nprint(dates_utc)\\n\\n# 3. Use closed parameter to control interval closure\\n\\nprint("n=== closed parameter comparison ===")\\n\\nprint("closed=None (default, both ends closed):", pd.date_range('2023-01-01','2023-01-03', freq='D', closed=None))\\n\\nprint("closed='left':", pd.date_range('2023-01-01','2023-01-03', freq='D', closed='left'))\\n\\nprint("closed='right':", pd.date_range('2023-01-01','2023-01-03', freq='D', closed='right'))\\n\\n# 4. Use normalize parameter to normalize time\\n\\nprint("n=== normalize parameter ===")\\n\\n dates_with_time = pd.date_range(start='2023-01-01 14:30:00', periods=3, freq='H')\\n\\nprint("Before normalization:", dates_with_time)\\n\\ndates_normalized = pd.date_range(start='2023-01-01 14:30:00', periods=3, freq='H', normalize=True)\\n\\nprint("After normalization:", dates_normalized)\\n\\n**Run Results:**\\n\\n=== withTimezonedate range ===DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03'], dtype='datetime64[ns, Asia/Shanghai]')Timezone: Asia/Shanghai=== UTC Timezone ===DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03'], dtype='datetime64[ns, UTC]')=== closed ParameterComparison === closed=None (Default, Both ends closed): DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03']) closed='left': DatetimeIndex(['2023-01-01', '2023-01-02']) closed='right': DatetimeIndex(['2023-01-02', '2023-01-03'])=== normalize Parameter ===Before normalization: DatetimeIndex(['2023-01-01 14:30:00', '2023-01-01 15:30:00', '2023-01-01 16:30:00'])After normalization: DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03'])\\n**Code Analysis:**\\n\\n* The `tz` parameter can specify time zone, very useful when handling cross-time-zone data.\\n* The `closed` parameter can control the closure of the date interval, default is both ends closed.\\n* `normalize=True` normalizes time to midnight, facilitating data aggregation by date.\\n\\n* * *\\n\\n## Notes\\n\\n> **Important Notes:**\\n> \\n> \\n> * You must specify `start` and `end`, or `start` and `periods`, or `end` and `periods`; you cannot specify only one.\\n> * Frequency abbreviations are case-sensitive, e.g., 'D' means day, 'd' will cause an error.\\n> * When handling time zone data, ensure all dates use a unified time zone, otherwise unexpected results may occur.\\n> * Be mindful of memory usage when generating a large number of dates (e.g., multi-year data).\\n\\n* * Pandas Common Functions](#)
YouTip