'Date transformation in time series

I have a month column which has values such as 202212, 202103, 201901. The entry 202212 means, December 2022. I want to transform all the month column in the dataset to date for example 202212 should become 01-12-2022, 202103 become 01-03-2021 an so on.



Solution 1:[1]

I would do it following way

import pandas as pd
df = pd.DataFrame({"d":["202212","202103","201901"]})
df['d2'] = pd.to_datetime(df['d'],format='%Y%m')
print(df)

output:

        d         d2
0  202212 2022-12-01
1  202103 2021-03-01
2  201901 2019-01-01

Explanation: both pandas.to_datetime and datetime.datetime.strptime does assume first day of month if it is not given, example showing such behavior of latter:

import datetime
d = datetime.datetime.strptime("202212","%Y%m")
print(d.year, d.month, d.day)  # 2022 12 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daweo