Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pandas datetime64[ns] to julian day

I am confused by the number of data type conversions and seemingly very different solutions to this, none of which I can get to work.

What is the best way to convert a pandas datetime column (datetime64[ns] eg 2017-01-01 03:15:00) to another column in the same pandas dataframe, converted to julian day eg 2458971.8234259?

Many thanks

like image 942
Michael K Avatar asked Oct 30 '25 11:10

Michael K


1 Answers

Create DatetimeIndex and convert to julian dates:

df = pd.DataFrame({'dates':['2017-01-01 03:15:00','2017-01-01 03:15:00']})
df['dates'] = pd.to_datetime(df['dates'])

df['jul1'] = pd.DatetimeIndex(df['dates']).to_julian_date()
#if need remove times
df['jul2'] = pd.DatetimeIndex(df['dates']).floor('d').to_julian_date()

print (df)
                dates          jul1       jul2
0 2017-01-01 03:15:00  2.457755e+06  2457754.5
1 2017-01-01 03:15:00  2.457755e+06  2457754.5

Because:

df['jul'] = df['dates'].dt.to_julian_date()

AttributeError: 'DatetimeProperties' object has no attribute 'to_julian_date'

like image 160
jezrael Avatar answered Nov 02 '25 16:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!