Python:将纳秒转换为DateTime64格式

Python:将纳秒转换为DateTime64格式,python,numpy,pandas,Python,Numpy,Pandas,我有一列时间戳,以秒为单位(从午夜开始),在数据帧df中精度为纳秒,如34200.934549345、34205.735545344,等等 这些时间戳来自同一天2011-01-10 如何以numpy的DateTime64格式将这些秒转换为纳秒精度 我想在我的df 2011-01-10 9:30:00.934549345 2011-01-10 9:30:05.735545344 我需要在这个问题的解决方案下进行精确的运算 有可能吗?我可以用datetime.strTime构造函数解析这个,但我必

我有一列
时间戳
,以秒为单位(从午夜开始),在数据帧
df
中精度为纳秒,如
34200.934549345、34205.735545344
,等等

这些
时间戳
来自同一天
2011-01-10

如何以
numpy
DateTime64
格式将这些秒转换为纳秒精度

我想在我的
df

2011-01-10 9:30:00.934549345
2011-01-10 9:30:05.735545344
我需要在这个问题的解决方案下进行精确的运算


有可能吗?

我可以用datetime.strTime构造函数解析这个,但我必须删掉字符串上的最后3个字符:

>>> ds
'2011-01-10 9:30:00.934549345'
>>> datetime.datetime.strptime(ds[:-3], '%Y-%m-%d %H:%M:%S.%f')
datetime.datetime(2011, 1, 10, 9, 30, 0, 934549)
似乎允许的最终粒度级别为,并且根据定义,必须在六位数以内:

>>> datetime.datetime(2011, 1, 10, 9, 30, 0, 934549345)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: microsecond must be in 0..999999
datetime.datetime(2011,1,10,9,30,0934549345) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ValueError:微秒必须在0..99999中 由于时间是以纳秒为单位的,如果要转换为Python datetime对象,则必须失去该精度级别,或者被迫创建自己的解决方法。

当给定一个以秒为单位的字符串并需要以微秒为单位显示时,我使用strtime()将以秒为单位。由于小数位数没有定义,我不得不处理所有的可能性。我不得不使用Python2.6.7,它要求strTime使用整数秒,并且不允许小数部分出现在字符串中。如果我有版本2.7.6,那么我可以使用格式的%f部分。但是,我仍然必须确保秒的小数部分只有6位数字

import datetime DT
def mystrptime(self, val)
  vals = val.split('.')
  if len(vals) == 1:
    dt = DT.datetime.strptime(val, '%Y-%m-%d %H%M%S')
  else:
    nofrag, frag = vals
    length = len(frag)
    if length > 6:
      frag = frag[:5]
      length = len(frag) # This resets length to 6, but is not really needed
    while length < 6:
      frag = frag + '0'
      length += 1
    nofrag_dt = DT.datetime.strptime(nofrag, '%Y-%m-%d %H%M%S')
    dt = nofrag_dt.replace(microsecond=int(frag))
  return dt
导入日期时间DT
def mystrptime(self,val)
val=val.split('.'))
如果len(vals)==1:
dt=dt.datetime.strtime(val,'%Y-%m-%d%H%m%S')
其他:
nofrag,frag=VAL
长度=长度(碎片)
如果长度>6:
碎片=碎片[:5]
length=len(frag)#这会将长度重置为6,但实际上并不需要
当长度<6时:
frag=frag+“0”
长度+=1
nofrag_dt=dt.datetime.strtime(nofrag,“%Y-%m-%d%H%m%S”)
dt=nofrag_dt.更换(微秒=int(frag))
返回dt
安装Python 2.7.6或更高版本后,可以按如下方式使用%f选项:

import datetime DT
def mystrptime(self, val)
  vals = val.split('.')
  if len(vals) > 1:
    nofrag, frag = vals
    frag = frag[:5] # This works even if frag is < 6 characters
    val = '.'.join(nofrag, frag)
  dt = DT.datetime.strptime(val, '%Y-%m-%d %H%M%S.%f')
  return dt
导入日期时间DT
def mystrptime(self,val)
val=val.split('.'))
如果len(VAL)>1:
nofrag,frag=VAL
frag=frag[:5]#即使frag小于6个字符,此选项也有效
val='.'.join(nofrag,frag)
dt=dt.datetime.strtime(val,%Y-%m-%d%H%m%S.%f')
返回dt

这就是你在使用之前想的:.astype('timedelta64[s]')?@AndyHayden,然后我意识到切掉了小数部分。如果您注意到在第一个示例中,日期被四舍五入到最接近的秒(我们有
2011-01-10 09:30:00
2011-01-10 09:30:05
)。这个版本是正确的。有趣的。。。!我想这是有道理的,现在我想起来了,这就是numpy处理pico seconds等的方式。而在熊猫中,ns是最小的。@U2EF1谢谢!但是为什么我会得到这个错误
TypeError:descriptor'\uuu add\uuuu'需要一个'datetime.datetime'对象,但在测试代码时收到了一个'float'
> df = pd.DataFrame({'seconds_since_midnight': [34200.934549345, 34205.735545344]})
> df['actual_date'] = (df.seconds_since_midnight * 1e9).astype('timedelta64[ns]') + pd.to_datetime('2011-01-10')
> df
   seconds_since_midnight                   actual_date
0            34200.934549 2011-01-10 09:30:00.934549345
1            34205.735545 2011-01-10 09:30:05.735545344

[2 rows x 2 columns]