consider array below
dt = DatetimeIndex(['2016-01-01', '2016-01-01', '2016-01-21', '2016-01-21'], dtype='datetime64[ns]', name=u'date', freq=None)
i convert the above to_julian_date()dtype
j = dt.to_julian_date()
Float64Index([2457388.5, 2457388.5, 2457408.5, 2457408.5], dtype='float64')
how can I convert jback to dt
I tried
dt = pd.to_datetime(j, errors = 'coerce')
it converts j back to datetime object, but the values are not the same, here is the output
DatetimeIndex(['1970-01-01 00:00:00.002457388',
'1970-01-01 00:00:00.002457388',
'1970-01-01 00:00:00.002457408',
'1970-01-01 00:00:00.002457408'],
dtype='datetime64[ns]', freq=None)
Julian dates are number of days from a long time ago. A time prior to the pandas Timestamp epoch. We refer to the beginning of when a system of tracking time begins tracking time. For Timestamp that is '1970-01-01'. What I do is get the Timestamp epoch with pd.to_datetime(0, unit='s'). That is the first possible pandas.Timestamp. Then I assign the number of days of that Timestamp from the beginning of the Julian Date by running to_julian_date() on that Timestamp. Now that I have the number of days from the beginning of the Julian start date to the Timestamp epoch, I subtract that from each of the Julian dates in j and that becomes the number of days from the Timestamp epoch (or '1970-01-01'). I can then use pd.to_datetime(j - epoch, unit='D') to give me the Timestamps where each of my values represent the number of days from the Timestamp epoch.
I hope that is clear ;-)
first find the julian_date for the pd.Timestamp epoch
epoch = pd.to_datetime(0, unit='s').to_julian_date()
then conversion is done with pd.to_datetime using the parameter unit='D'
pd.to_datetime(j - epoch, unit='D')
DatetimeIndex(['2016-01-01', '2016-01-01', '2016-01-21', '2016-01-21'], dtype='datetime64[ns]', freq=None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With