Skip to content

BUG: pd.to_datetime with format doesn't work with pd.NA #42957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
levkovalenko opened this issue Aug 9, 2021 · 2 comments · Fixed by #42982
Closed
3 tasks done

BUG: pd.to_datetime with format doesn't work with pd.NA #42957

levkovalenko opened this issue Aug 9, 2021 · 2 comments · Fixed by #42982
Assignees
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Milestone

Comments

@levkovalenko
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import numpy as np
import pandas as pd

a = pd.DataFrame({'a':[pd.NA]})  
b = pd.DataFrame({'b': [np.nan]})
pd.to_datetime(b['b'])                              # -> Work
pd.to_datetime(b['b'], format="%Y%m%d%H%M%S")       # -> Work
pd.to_datetime(a['a'])                              # -> Work
pd.to_datetime(a['a'], format="%Y%m%d%H%M%S")       # -> Error

Problem description

I try to use pd.NA to define NaN values in field what contains datetime strings.
I try convert them to datetime, but it doesn't work.
I get error ValueError: time data '<NA>' does not match format '%Y%m%d%H%M%S' (match)

Actually the same code with np.nan works correctly

Expected Output

pd.NaT with no errors

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.18.0-305.7.1.el8_4.x86_64
Version : #1 SMP Mon Jun 14 17:25:42 EDT 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.utf8
LOCALE : en_US.UTF-8

pandas : 1.3.1
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.2
setuptools : 49.6.0.post20210108
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

@levkovalenko levkovalenko added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 9, 2021
@jorisvandenbossche jorisvandenbossche added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 9, 2021
@debnathshoham
Copy link
Member

take

@levkovalenko
Copy link
Author

@debnathshoham

Hi, i see your PR

It doesn't fix BUG. I'm sorry for bad desription, but i have bug with this code:

import numpy as np
import pandas as pd

a = pd.DataFrame({'a':[pd.NA, "20210202202020"]})  
b = pd.DataFrame({'b': [np.nan, "20210202202020"]})
print(pd.to_datetime(b['b'], format="%Y%m%d%H%M%S"))      # -> Work
# 0                   NaT
# 1   2021-02-02 20:20:20
# Name: b, dtype: datetime64[ns]
print(pd.to_datetime(a['a'], format="%Y%m%d%H%M%S"))       # -> Error
#ValueError: time data '<NA>' does not match format '%Y%m%d%H%M%S' (match)

So datetime formatter can't work with pd.NA values but for np.nan everything is good.

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Aug 11, 2021
@jreback jreback added this to the 1.4 milestone Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants