无法使用钟摆来解析系列中的日期,但可以一个一个地工作

Can't use pendulum to parse dates in Series, but works one by one

我正在尝试使用 pendulum 解析日期。我有一个 TimeStamp 约会对象,所以我做了以下事情:

df['aux']=df['Date'].dt.date
df['p_date']=df.aux.apply(lambda x: pendulum.parse(x))

这会带来以下错误:

AttributeError: 'DateTime' object has no attribute 'nanosecond'

但如果我这样做,就像:

pendulum.parse(df.aux[0])

解析没问题。我以为 apply(lambda x:)Series 的所有行应用了相同的函数,但现在它不起作用了。发生什么事了?

示例代码:

dates=pd.Series(['2018-03-20','2019-03-21'])
dates.apply(lambda x: pendulum.parse(x)) #Doesn't work
pendulum.parse(dates[0]) #Works

由于pandas在github中没有nanosecond,因此将其转换为str,而不是收到以下错误

'DateTime' object has no attribute 'nanosecond'

dates.apply(lambda x: str(pendulum.parse(x)))
Out[256]: 
0    2018-03-20T00:00:00+00:00
1    2019-03-21T00:00:00+00:00
dtype: object

我认为你必须在最后使用 .naive()

dates.apply(lambda x: pendulum.parse(x).naive()) #works

查看此线程:https://github.com/sdispater/pendulum/issues/246

It seems that pandas tries to convert the timezone aware datetime to it's own timestamp representation, but that conversion isn't triggered with the naive datetime. I don't think anyone is at fault here, as a user of both pendulum and pandas it makes it difficult to use them together.