简体   繁体   English

转换熊猫系列和日期时间对象

[英]Converting pandas series and datetime objects

I have a series of dates with format: 我有一系列日期,格式如下:

df['myDateTimes']
0                       NaT
1       2017-07-23 00:26:50
2                       NaT
3       2017-07-31 04:07:24

(where the first number is just the pandas dataframe index) and I'd like to convert these to Modified Julian Dates (第一个数字只是pandas数据框索引),我想将它们转换为Modified Julian Dates

from datetime import datetime, timedelta
import julian
import datetime
dtime  = julian.to_jd(df['myDateTimes'], fmt='jd')

just gives a 只是给一个

AttributeError: 'Series' object has no attribute 'month'

df['myDateTimes'] is a pandas series and (I think) julian.to_jd needs a datetime.datetime object. df ['myDateTimes']是熊猫系列,(我认为)julian.to_jd需要一个datetime.datetime对象。

Using apply , since you have NaT julian will not expected that , so we filter before we transfer the datetime to julian 使用apply ,因为您有NaT julian不会这样,所以我们在将日期时间转移到julian之前进行过滤

s[s.notna()].apply(lambda x : julian.to_jd(x,fmt='jd')).reindex(s.index)
Out[139]: 
0             NaN
1    2.457958e+06
2             NaN
3    2.457966e+06
dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM