I have one field that has Interger format and I want that to be converted as date format (YYYYMM). entries are 201801 it should appear as 201801 but as date format ( original one is Int 64 format) because I want to plot it on a chart. If I use as Chart is not coming in proper way.
Asked
Active
Viewed 1,118 times
0
-
PS: I am working on Pandas in python – Ashish Feb 16 '21 at 18:50
-
1Please post some sample data with expected output. – Mayank Porwal Feb 16 '21 at 18:53
-
So you want to convert it to a timestamp? – Luca Sans S Feb 16 '21 at 18:53
-
202002 202001 output should also be same only format should be date format, currently it is int64 – Ashish Feb 16 '21 at 18:59
-
i don't want time stamp as I don't want time portion I want only YYYYMM format to be read – Ashish Feb 16 '21 at 18:59
1 Answers
0
You can represent dates with the .dt
method of the Series
Adapted from my similar answer https://stackoverflow.com/a/66038817/4541045
import pandas as pd
df = pd.DataFrame({"dates": [200101, 200202, 202010]})
# dates
# 0 200101
# 1 200202
# 2 202010
df.dates = pd.to_datetime(df.dates, format="%Y%m") # make a datetime
# dates
# 0 2001-01-01
# 1 2002-02-01
# 2 2020-10-01
df.dates.dt.strftime("%Y%m") # represent datetime Series
# 0 200101
# 1 200202
# 2 202010
# Name: dates, dtype: object
NOTE df.dates
is just referring to the column named dates
in this case

ti7
- 16,375
- 6
- 40
- 68
-
-
1@Ashish - one can easily change the format of the chart tick labels, which seems like your actual end goal. – BigBen Feb 16 '21 at 19:04
-
Indeed - just change the args to the equivalent of `Series.dt.strftime()`! (probably `%Y%m`) – ti7 Feb 16 '21 at 19:05
-
no it did not work, it shows syntax error. I tried following '''dt2['ReportFiscalMonth']= dt2['ReportFiscalMonth'].dt.strftime(%Y%m)''' – Ashish Feb 16 '21 at 19:09
-
1@ti7 it worked thanks a lot '''dt2['ReportFiscalMonth'] = pd.to_datetime(dt2['ReportFiscalMonth'], format="%Y%m")''' – Ashish Feb 16 '21 at 19:14
-