YouTip LogoYouTip

Pandas Series Dt Month

[![Image 1: Pandas Common Functions](#) Pandas Common Functions](#) * * * `Series.dt.month` is an attribute in Pandas used to **extract the month from a datetime**. It is part of the dt accessor, allowing you to quickly extract month information (1-12) from a datetime Series. In time series data analysis, analyzing by month is a common requirement, such as for monthly reports, seasonal analysis, etc. The `dt.month` attribute makes these operations simple and efficient. **Word Definition**: `month` means "month", which returns the month part of a date (1 to 12). * * * ## Basic Syntax and Parameters `Series.dt.month` is an attribute of the Series dt accessor used to extract the month. ### Syntax Format Series.dt.month ### Parameter Description This attribute does not require any parameters; it directly accesses the month information of a datetime Series. ### Return Value Description * **Return Value**: Returns an integer Series containing the month (1-12). * **Effect**: Extracts the month part from a datetime64 Series, returning integers from 1 to 12. * * * ## Examples Let's thoroughly master the usage of `Series.dt.month` through a series of examples ranging from simple to complex. ### Example 1: Basic Usage - Extracting the Month ## Example import pandas as pd # 1. Create a datetime Series print("=== Create datetime Series ===") dates = pd.Series([ '2023-01-15', '2023-05-20', '2023-11-30', '2023-07-10', '2023-03-25' ]) # Convert to datetime type datetime_series = pd.to_datetime(dates) print("Original dates:") print(datetime_series) # 2. Use dt.month to extract month print("n=== Use dt.month to extract month ===") months = datetime_series.dt.month print("Months:") print(months) # 3. Can also use dt.month_name() to get month names print("n=== Use dt.month_name() to get month names ===") month_names = datetime_series.dt.month_name() print(month_names) # 4. Chinese month names (requires localization) print("n=== Month abbreviations ===") month_abbrev = datetime_series.dt.month_name().str[:3] print(month_abbrev) **Output:** === Create datetime Series ===0 2023-01-151 2023-05-202 2023-11-303 2023-07-104 2023-03-25 dtype: datetime64=== Use dt.month to extract month ===Months:0 11 52 113 74 3 dtype: int64 === Use dt.month_name() to get month names ===Month names:0 January1 May2 November3 July4 March dtype: object=== Month abbreviations ===0 Jan1 May2 Nov3 Jul4 Mar dtype: object **Code Explanation:** 1. `dt.month` returns an integer from 1-12, where 1 represents January and 12 represents December. 2. `dt.month_name()` returns the full English name of the month. 3. You can get the month abbreviation through string slicing. ### Example 2: Filtering and Grouping by Month ## Example import pandas as pd import numpy as np # Create sales data print("=== Create sales data ===") df = pd.DataFrame({ 'order_id': [f'ORD-{i:04d}'for i in range(1,25)], 'order_date': pd.date_range('2023-01-01', periods=24, freq='15D'), 'sales': np.random.randint(1000,5000,24) }) # Extract month df['month']= df['order_date'].dt.month print(df.head(10)) # Filter by month - filter Q1 data print("n=== Filter Q1 (Jan-Mar) orders ===") q1_orders = df[df['month'].is
← Pandas Series Dt WeekdayPandas Pd Timedelta β†’