pythonpandasdatependulum

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


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

Solution

  • 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.