pd datetime
2 min readMar 3, 2023
Basic dataframe
import pandas as pd
import numpy as np
dates = [str(y)+"-" +str(m)+"-"+str(d) for y in range(2000, 2001) for m in range(1, 13) for d in range(1, 20)]
df = pd.DataFrame({"date": dates})
Change string to datetime
>>> df["date_datetime"] = pd.to_datetime(df["date"])
>>> df.dtypes
date object
date_datetime datetime64[ns]
Set a missing value
>>> df.iloc[0, 1] = np.NaN
>>> df
date date_datetime
0 2000-1-1 NaT
1 2000-1-2 2000-01-02
2 2000-1-3 2000-01-03
3 2000-1-4 2000-01-04
4 2000-1-5 2000-01-05
.. ... ...
223 2000-12-15 2000-12-15
224 2000-12-16 2000-12-16
225 2000-12-17 2000-12-17
226 2000-12-18 2000-12-18
227 2000-12-19 2000-12-19
Change datetime to the first day of a month [1]
>>> df["year_month_firstDay"] = df['date_datetime'].to_numpy().astype('datetime64[M]')
>>> df
date date_datetime year_month_firstDay
0 NaN NaT NaT
1 2000-1-2 2000-01-02 2000-01-01
2 2000-1-3 2000-01-03 2000-01-01
3 2000-1-4 2000-01-04 2000-01-01
4 2000-1-5 2000-01-05 2000-01-01
.. ... ... ...
223 2000-12-15 2000-12-15 2000-12-01
224 2000-12-16 2000-12-16 2000-12-01
225 2000-12-17 2000-12-17 2000-12-01
226 2000-12-18 2000-12-18 2000-12-01
227 2000-12-19 2000-12-19 2000-12-01
[228 rows x 3 columns]
>>> df.dtypes
date object
date_datetime datetime64[ns]
year_month_firstDay datetime64[ns]
dtype: object
Change datetime to the following month of the same day [2]
>>> df["year_nextMonth_firstDay"] = df["year_month_firstDay"] + pd.DateOffset(months=1)
>>> df
date date_datetime year_month_firstDay year_nextMonth_firstDay
0 2000-1-1 NaT NaT NaT
1 2000-1-2 2000-01-02 2000-01-01 2000-02-01
2 2000-1-3 2000-01-03 2000-01-01 2000-02-01
3 2000-1-4 2000-01-04 2000-01-01 2000-02-01
4 2000-1-5 2000-01-05 2000-01-01 2000-02-01
.. ... ... ... ...
223 2000-12-15 2000-12-15 2000-12-01 2001-01-01
224 2000-12-16 2000-12-16 2000-12-01 2001-01-01
225 2000-12-17 2000-12-17 2000-12-01 2001-01-01
226 2000-12-18 2000-12-18 2000-12-01 2001-01-01
227 2000-12-19 2000-12-19 2000-12-01 2001-01-01
[228 rows x 4 columns]
>>> df.dtypes
date object
date_datetime datetime64[ns]
year_month_firstDay datetime64[ns]
year_nextMonth_firstDay datetime64[ns]
dtype: object
Pandas version used to build those test cases
>>> pd.__version__
'1.3.2'
Reference
[1] Extracting the first day of month of a datetime type column in pandas
[2] https://www.plus2net.com/python/pandas-dt-dateoffset.php