camphor
camphor

Reputation: 71

How to check index of a DataFrame for first day of the month

If I have a DataFrame indexed by datetimes, how can I select the rows corresponding to the first day of each month, or check if a given row is at the first day of a month?

Upvotes: 1

Views: 621

Answers (2)

Scott Boston
Scott Boston

Reputation: 153550

Here is another way, use the is_month_start attribute with boolean indexing:

df[df.index.is_month_start]

Technically, this should be the fastest way, since this is already an attribute of the DateTimeIndex.

Upvotes: 1

jezrael
jezrael

Reputation: 863741

Select all rows with first day by compare DatetimeIndex.day in boolean indexing:

df[df.index.day == 1]

Upvotes: 4

Related Questions