Skip to content

BUG: inconsistent None handling for ISO and non-ISO formats #50071

Closed
@MarcoGorelli

Description

@MarcoGorelli

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

In [15]: to_datetime(['2020-01-01 00:00:00+01:00', '2020-01-01 00:00:00+02:00', None], format='%Y-%m-%d %H:%M:%S%z')
Out[15]: Index([2020-01-01 00:00:00+01:00, 2020-01-01 00:00:00+02:00, None], dtype='object')

In [16]: to_datetime(['2020-01-01 00:00:00+01:00', '2020-01-01 00:00:00+02:00', None], format='%Y-%d-%m %H:%M:%S%z')
Out[16]: Index([2020-01-01 00:00:00+01:00, 2020-01-01 00:00:00+02:00, NaT], dtype='object')

Issue Description

In the first (ISO) case, None stays None. But in the second (non-ISO) case, None becomes NaT.

Expected Behavior

@jbrockmendel any thoughts on what should be correct?

I think I'd prefer the latter (just change to NaT)

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.8.15.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.3.1
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.1.2
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions