0

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.

Ashish
  • 115
  • 3
  • 15

1 Answers1

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