Zanam
Zanam

Reputation: 4807

Python pandas generating first day of month from array of datetime

I am trying to obtain the first day of month from array of datetime i.e. change all days to 1 and all hours to 0:

import pandas as pd
z1 = [datetime(2025, 10, 1, 3, 0),datetime(2025, 1, 6, 7, 0)]
pd.DatetimeIndex(z1).normalize()
DatetimeIndex(['2025-10-01', '2025-01-06'], dtype='datetime64[ns]', freq=None)

I was hoping to achieve

DatetimeIndex(['2025-10-01', '2025-01-01'], dtype='datetime64[ns]', freq=None)

Upvotes: 2

Views: 3059

Answers (3)

harmony
harmony

Reputation: 111

use date_range and set freq = 'MS'. The meaning of 'MS' can be interpreted from below

enter image description here

One line of code:

date_series = pd.date_range(start='1/1/2017', end ='12/1/2019', freq='MS')

Upvotes: 3

unutbu
unutbu

Reputation: 879143

Another way would be to form a NumPy array of dtype datetime64[M] (a datetime64 with monthly resolution)

In [31]: np.array(z1, dtype='datetime64[M]')
Out[31]: array(['2025-10', '2025-01'], dtype='datetime64[M]')

Passing it to pd.DatetimeIndex returns

In [32]: pd.DatetimeIndex(np.array(z1, dtype='datetime64[M]'))
Out[32]: DatetimeIndex(['2025-10-01', '2025-01-01'], dtype='datetime64[ns]', freq=None)

Upvotes: 4

jezrael
jezrael

Reputation: 862481

You can first create Series from z1, then replace day and convert to date:

print (pd.DatetimeIndex(pd.Series(z1).apply(lambda x: x.replace(day=1)).dt.date))
DatetimeIndex(['2025-10-01', '2025-01-01'], dtype='datetime64[ns]', freq=None)

Another solution is convert day and hour:

print (pd.DatetimeIndex(pd.Series(z1).map(lambda x: x.replace(day=1, hour=0))))
DatetimeIndex(['2025-10-01', '2025-01-01'], dtype='datetime64[ns]', freq=None)

Upvotes: 2

Related Questions