Home > Back-end >  Infer date from number in pandas series
Infer date from number in pandas series

Time:08-04

I have column in a pandas DataFrame called df['latestCertificationDate']

It contains a series of numbers like this:

0       1570406400000
1       1479427200000
2       1506556800000
3       1527724800000
4       1490140800000
            ...      
4630    1473811200000
4631    1601337600000
4632    1585267200000
4633    1467158400000
4634    1484697600000
Name: initialCertificationDate, Length: 4635, dtype: int64

I know that each entry relates to a date. I have one reference that shows me the first entry 1570406400000 translates to October 2019.

Is there a way for me to infer the date or timestamp format and convert every entry in the series to the Month Year format?

I've tried using date.fromtimestamp() and it does seem to give the right output.

Thanks for any help you can offer!

CodePudding user response:

pd.to_datetime(1570406400000)   pd.tseries.offsets.DateOffset(months=597)

Output:

Timestamp('2019-10-01 00:26:10.406400')

CodePudding user response:

using fromtimestamp of datetime and removing the last three zeros from the number, I get the following dates.

is that what you're looking for?

df['time'].apply(lambda x: datetime.datetime.fromtimestamp((x//1000)) ).to_frame()
        time
0       2019-10-06 20:00:00
1       2016-11-17 19:00:00
2       2017-09-27 20:00:00
3       2018-05-30 20:00:00
4       2017-03-21 20:00:00
4630    2016-09-13 20:00:00
4631    2020-09-28 20:00:00
4632    2020-03-26 20:00:00
4633    2016-06-28 20:00:00
4634    2017-01-17 19:00:00
  • Related