pandas.to_datetime() format bug?

Viewed 28

I am converting a date using pd.to_datetime() e.g. "20130526T150000" "20130526T"

Btw, this can be done directly as it is more or less iso standard:

print (pd.to_datetime("20130526T150000"))
print (pd.to_datetime("20130526T"))
2013-05-26 15:00:00
2013-05-26 00:00:00

However, suppose for a moment I really want to specify the format... I can do this using the right format for "any" format:

print (pd.to_datetime("20130526foobar",format='%Y%m%dfoobar'))
2013-05-26 00:00:00

But for

print (pd.to_datetime("20130526T",format='%Y%m%dT'))

I get ValueError: time data 20130526T doesn't match format specified What is going on here????

Also, If you can easily check please leave a comment: I am using pyton3.7, pandas version 1.3.5, is this still happening in the case for the latest pandas(1.4?)

Finally, the following behavior makes no sense to me and makes me think there is something going on with %d:

In [23]: print (pd.to_datetime("2013-05-11T" ,format='%Y-%m-%dT'))

TypeError                                 Traceback (most recent call   
.........................
ValueError: time data 2013-05-11T doesn't match format specified

In [24]: print (pd.to_datetime("2013-05-11T" ,format='%Y-%m-%HT'))
2013-05-01 11:00:00

In [25]: print (pd.to_datetime("2013-05-11T00:00:00" ,format='%Y-%m-%dT'))
2013-05-11 00:00:00

In [26]: print (pd.to_datetime("2013-05-11T00:00:00" ,format='%Y-%m-%HT'))
TypeError                                 Traceback (most recent call last)
.........................
ValueError: unconverted data remains: 00:00:00
0 Answers
Related