I'm trying to convert pandas dataframe into SPSS format and have an issue converting a datetime64 variable.
Using the following codes:
import pandas as pd
import datetime as dt
df['date1'] = (df['date'] - pd.Timestamp('1582-10-15 00:00')).astype('timedelta64[s]')
or
df['date1'] = (df['date'] - dt.datetime(1582, 10, 15)).astype('timedelta64[s]')
I get Out of bounds nanosecond timestamp: 1582-10-15 00:00:00
error.
When I just try for fun to use 1982 instead, it works!
I know there is a hard way to go to epoch from 1582 to 1970 and utc time etc, but is there an easy way? Thank you very much!
I believe Timestamp
breaks on such a long ago date because there is no record of how to handle the various leap seconds and what not all along the way. So the error you're getting is the breaking down of the finest precision of time. That makes sense. It's saying that it can't be that precise with a date that long ago.
Use dt.datetime instead. It doesn't require as much precision.
import pandas as pd
import datetime as dt
epoch = dt.datetime(1582, 10, 15)
date = dt.datetime(2016, 3, 31)
int((date - epoch).total_seconds())