Python Pandas - Indicate whether the date in DateTimeIndex is the first day of the month

To check whether the date in DateTimeIndex is the first day of the month, use the DateTimeIndex.is_month_start property. This boolean property returns True for dates that fall on the first day of their respective months.

Syntax

DateTimeIndex.is_month_start

Creating a DateTimeIndex

First, let's create a DateTimeIndex with various dates to demonstrate the functionality ?

import pandas as pd

# Create a DateTimeIndex with period 6 and frequency as 5 days
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-9-21 02:30:50', periods=6, tz='Australia/Adelaide', freq='5D')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-09-21 02:30:50+09:30', '2021-09-26 02:30:50+09:30',
               '2021-10-01 02:30:50+09:30', '2021-10-06 02:30:50+10:30',
               '2021-10-11 02:30:50+10:30', '2021-10-16 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='5D')

Checking First Day of Month

Now we can use the is_month_start property to identify which dates are the first day of their respective months ?

import pandas as pd

# Create DateTimeIndex
datetimeindex = pd.date_range('2021-9-21 02:30:50', periods=6, tz='Australia/Adelaide', freq='5D')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)

# Check whether the date in DateTimeIndex is the first day of the month
print("\nCheck whether the date in DateTimeIndex is the first day of the month...")
print(datetimeindex.is_month_start)
DateTimeIndex...
DatetimeIndex(['2021-09-21 02:30:50+09:30', '2021-09-26 02:30:50+09:30',
               '2021-10-01 02:30:50+09:30', '2021-10-06 02:30:50+10:30',
               '2021-10-11 02:30:50+10:30', '2021-10-16 02:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='5D')

DateTimeIndex frequency...
<5 * Days>

Check whether the date in DateTimeIndex is the first day of the month...
[False False  True False False False]

Example with Month Start Dates

Let's create another example that includes more month start dates to better demonstrate the property ?

import pandas as pd

# Create DateTimeIndex with specific dates including month starts
dates = ['2021-10-01', '2021-10-15', '2021-11-01', '2021-11-30', '2021-12-01']
datetimeindex = pd.DatetimeIndex(dates)

print("DateTimeIndex...")
print(datetimeindex)

print("\nFirst day of month check...")
print(datetimeindex.is_month_start)

# Show which specific dates are month starts
month_starts = datetimeindex[datetimeindex.is_month_start]
print("\nDates that are first day of month:")
print(month_starts)
DateTimeIndex...
DatetimeIndex(['2021-10-01', '2021-10-15', '2021-11-01', '2021-11-30',
               '2021-12-01'],
              dtype='datetime64[ns]', freq=None)

First day of month check...
[ True False  True False  True]

Dates that are first day of month:
DatetimeIndex(['2021-10-01', '2021-11-01', '2021-12-01'], dtype='datetime64[ns]', freq=None)

Conclusion

The is_month_start property provides a simple way to identify dates that fall on the first day of their respective months. It returns a boolean array where True indicates the date is a month start, making it useful for time series analysis and filtering operations.

Updated on: 2026-03-26T17:09:44+05:30

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements