-1

I have datasets which have the dates in the format of yyyymmdd. For example, May 5th, 2018 would be 20180505.

How can I reformat these entries such that they will be a time series in pandas?

paulnsn
  • 107
  • 2
  • 11
  • Possible duplicate of [Convert integer (YYYYMMDD) to date format (mm/dd/yyyy) in python](https://stackoverflow.com/questions/43133605/convert-integer-yyyymmdd-to-date-format-mm-dd-yyyy-in-python) – rafaelc May 06 '18 at 06:30
  • 1
    Possible duplicate of [How to convert integer into date object python?](https://stackoverflow.com/questions/9750330/how-to-convert-integer-into-date-object-python) – harvpan May 06 '18 at 15:30

3 Answers3

1

Try this:

from datetime import datetime
a = '20180505'
date = datetime.strptime(a, '%Y%m%d').strftime('%m/%d/%Y')

From this link: Convert integer (YYYYMMDD) to date format (mm/dd/yyyy) in python

iRhonin
  • 383
  • 3
  • 14
0

You could find everything on pandas docs. Read about datetime in pandas,

## sample data
df = pd.DataFrame({'dates': ['20180505','20180506','20180507','20180508']})

## convert to dates
df['dates'] = pd.to_datetime(df['dates'])
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

You may want also to change the format and this is an example:

df = pd.DataFrame({'dates': ['20180505','20180506','20180507','20180508']})
print df
      dates
0  20180505
1  20180506
2  20180507
3  20180508
df['dates'] =  pd.to_datetime(df['dates'], format='%Y%m%d').dt.strftime('%d/%b/%Y')
print df

Output:

         dates
0  05/May/2018
1  06/May/2018
2  07/May/2018
3  08/May/2018
Joe
  • 12,057
  • 5
  • 39
  • 55