0

I have a pandas series with normal dates, say 2017-01-01. I'm trying to call it in a lambda function, but if I cal row['date'], it returns '2017-01-01T00:00:00.000000000'. What is the right format to convert it to a datetime using strptime?

Appreciate the help

Dick Thompson
  • 599
  • 1
  • 12
  • 26
  • is your Series in type of datetime? Maybe `pd.to_datetime(s, format='%Y%m%d')` ?? – ipramusinto Sep 20 '18 at 00:53
  • don't think it's really necessary- the only relevant datapoint is the date string '2017-01-01T00:00:00.000000000' how do I convert that to datetime using strptime? – Dick Thompson Sep 20 '18 at 04:41

1 Answers1

1

Data:

df = pd.DataFrame({'date':['2017-01-01T00:00:00.000000000','2017-01-02T00:00:00.000000000','2017-01-03T00:00:00.000000000']})

You can convert the whole column with pd.to_datetime.

>>> pd.to_datetime(df['date'])
0   2017-01-01
1   2017-01-02
2   2017-01-03
Name: date, dtype: datetime64[ns]

>>> df['date'][0]
Timestamp('2017-01-01 00:00:00')

If you have an individual cell, you can also use pd.to_datetime

>>> pd.to_datetime('2017-01-01T00:00:00.000000000')
Timestamp('2017-01-01 00:00:00')

And then strftime if needed:

>>> pd.to_datetime('2017-01-01T00:00:00.000000000').strftime('%Y/%m/%d')
'2017/01/01'

But converting individual cell is faster with pd.Timestamp:

>>> %timeit pd.Timestamp('2017-01-01T00:00:00.000000000')
2.18 µs ± 8.56 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

>>> %timeit pd.to_datetime('2017-01-01T00:00:00.000000000')
54 µs ± 560 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
hellpanderr
  • 5,581
  • 3
  • 33
  • 43
  • Is there no way to use strptime? I'm calling the date in a lambda function and it returns it to me in a weird format. When I try to do apply(lambda x: pd.to_datetime(x['date']) it gives me the error ValueError: ('Unknown string format', 'occurred at index 0') – Dick Thompson Sep 20 '18 at 23:55
  • Can you print its value in a function? – hellpanderr Sep 21 '18 at 04:45