I have a DataFrame looking like this
Sigma JulianDay
0 -9.05 2.451545e+06
1 -10.99 2.451545e+06
2 -8.42 2.451546e+06
3 -8.92 2.451547e+06
4 -10.79 2.451547e+06
5 -9.53 2.451548e+06
I want to convert the column 'JulianDay' to Gregorian Date. For that, I used the jdcal 1.3 package with the already defined function jd_to_date(jd)
d = df['JulianDay'].apply(jd_to_date)
df['GregDate'] = d
and the result is:
Sigma JulianDay GregDate
0 -9.05 2.451545e+06 (2000, 1, 1)
1 -10.99 2.451545e+06 (2000, 1, 1)
2 -8.42 2.451546e+06 (2000, 1, 2)
3 -8.92 2.451547e+06 (2000, 1, 3)
4 -10.79 2.451547e+06 (2000, 1, 3)
5 -9.53 2.451548e+06 (2000, 1, 4)
The output also tells me, that I am dealing with an object type:
Name: JulianDay, dtype: object
My problem is now, that I want to convert the GregDate column to datetime, so that I am able to set the GregDate as index. But the dtype: object gives me trouble when trying to
df['GregDate'] = pd.to_datetime(d)
I get the error:
TypeError: <class 'tuple'> is not convertible to datetime
So, how do I first convert the "tuple" column to something else, so that I am able to convert it to datetime?
Thanks
You can create new DataFrame
with constructor
and then apply to_datetime
, important are column names year
, month
and day
:
a = pd.DataFrame(df['GregDate'].values.tolist(), columns=['year','month','day'])
print (a)
year month day
0 2000 1 1
1 2000 1 1
2 2000 1 2
3 2000 1 3
4 2000 1 3
5 2000 1 4
df.GregDate = pd.to_datetime(a)
print (df)
Sigma JulianDay GregDate
0 -9.05 2451545.0 2000-01-01
1 -10.99 2451545.0 2000-01-01
2 -8.42 2451546.0 2000-01-02
3 -8.92 2451547.0 2000-01-03
4 -10.79 2451547.0 2000-01-03
5 -9.53 2451548.0 2000-01-04
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