1

I'm trying to parse dates using pendulum. I have a TimeStamp date, so I did the following:

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

Which brings the following error:

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

But if I do, something like:

pendulum.parse(df.aux[0])

It gets parsed no problem. I thought apply(lambda x:) applied the same function to all rows of the Series , but now it isn't working. What's happening?

Sample code:

dates=pd.Series(['2018-03-20','2019-03-21'])
dates.apply(lambda x: pendulum.parse(x)) #Doesn't work
pendulum.parse(dates[0]) #Works
Juan C
  • 5,846
  • 2
  • 17
  • 51

2 Answers2

0

Since pandas dose not have nanosecond , in github, so convert it to str, instead of received following error

'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
BENY
  • 317,841
  • 20
  • 164
  • 234
0

I think you have to use .naive() at the end

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

See this thread: 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.

Beauregard D
  • 107
  • 5