Reputation: 4807
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
Reputation: 111
use date_range
and set freq = 'MS'
. The meaning of 'MS'
can be interpreted from below
One line of code:
date_series = pd.date_range(start='1/1/2017', end ='12/1/2019', freq='MS')
Upvotes: 3
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
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