Pandas gregorian date to julian

Viewed 455

I'm facing a problem in converting dates in my dataframe.

Example:

        col1                 col2
1     2018-10-02            2018-07-03 
      09:00:00+00           23:56:09.134+00
2     2018-07-03            2018-10-02
      23:56:09.134+00       09:00:00+00           


df.dtypes
col1    object
col2    object

Due to the fact that I need to do some ML on the dataset, I need to transform the dates into a float, so in the julian format.

I tried a lot of things like: Calculating julian date in python or

df['col1'] = df['col1'].dt.strftime("%y%j")

The example above works well after having converted the column to_datetime, but it raises an error if I pass df[['col1', "col2"]] :

AttributeError: 'DataFrame' object has no attribute 'dt'

I've also problem with the format, since some obs does have milliseconds and some doesn't. I can drop them I think, but also in this case I don't know how.

Also I'm not able to find a julian format which is extended up to the seconds (%y%j is not enough, and I don't know which letters of the format I need)

I have many other columns with dates in my df, so there is a simple way to convert all of them?

Thanks

1 Answers

Are you looking for pandas.Timestamp.to_julian_date?

import pandas as pd

df = pd.DataFrame({'col1': ['2018-10-02 09:00:00+00','2018-07-03 23:56:09.134+00'],
                   'col2': ['2018-07-03 23:56:09.134+00','2018-10-02 09:00:00+00']})


df['col1'] = pd.to_datetime(df['col1'])
df['col2'] = pd.to_datetime(df['col2'])

df['col1'].apply(pd.Timestamp.to_julian_date)
# 0    2.458394e+06
# 1    2.458303e+06
# Name: col1, dtype: float64

The returned floating point number represents number of days, e.g.

df['col3'] = pd.to_datetime(['2018-07-02 12:46:32.257000+00:00', '2018-07-02 13:02:15.855000+00:00'])
t = df['col3'].apply(pd.Timestamp.to_julian_date).values
print(f"col3 delta in minutes: {(t[1]-t[0])*24*60}")
# col3 delta in minutes: 15.726633667945862
Related