Skip to content

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

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
MarcoGorelli opened this issue Dec 5, 2022 · 12 comments · Fixed by #50242
Closed
3 tasks done

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

MarcoGorelli opened this issue Dec 5, 2022 · 12 comments · Fixed by #50242
Labels
Bug Datetime Datetime data dtype Needs Discussion Requires discussion from core team before further action
Milestone

Comments

@MarcoGorelli
Copy link
Member

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

@MarcoGorelli MarcoGorelli added Bug Datetime Datetime data dtype Needs Discussion Requires discussion from core team before further action labels Dec 5, 2022
@MarcoGorelli
Copy link
Member Author

@mroeschke do you have thoughts on which is correct?

@jbrockmendel
Copy link
Member

i think id expect both to be NaT and both to be DatetimeIndex

@MarcoGorelli
Copy link
Member Author

I don't think DatetimeIndex is possible with mixed offsets, is it? (note how the strings have different offsets)

@jbrockmendel
Copy link
Member

didnt notice that, you're right

@mroeschke
Copy link
Member

Should the errors keyword be applicable here?

Since there's an implicit to_datetime(..., format=..., errors="raise") shouldn't this raise since format can't be applied to None? Not sure if we have testing for None being special

@MarcoGorelli
Copy link
Member Author

yeah it's tested here

vals = [
"nan",
Timestamp("1990-01-01"),
"2015-03-14T16:15:14.123-08:00",
"2019-03-04T21:56:32.620-07:00",
None,
]
ser = Series(vals)
assert all(ser[i] is vals[i] for i in range(len(vals))) # GH#40111
mixed = to_datetime(ser)
expected = Series(
[
"NaT",
Timestamp("1990-01-01"),
Timestamp("2015-03-14T16:15:14.123-08:00").to_pydatetime(),
Timestamp("2019-03-04T21:56:32.620-07:00").to_pydatetime(),
None,
],
dtype=object,
)

I just think expected is wrong on two counts:

  • the first "NaT" should be NaT
  • the None should be NaT

@mroeschke
Copy link
Member

I guess in that particular example format is not provided thought.

I haven't looked at mixed-argument to_datetime testing in a while but leads me to 2 questions

  1. If format is provided, should all arguments be strings (or datetimes that were recently added?) or can other arguments be parsed with non-formatting paths?
  2. Should in theory a None.strftime(format) -> NaT parsing be supported? Since NaT.strp/stftime are designed to raise errors my initial thought is no but could be convinced otherwise

@mroeschke
Copy link
Member

I just think expected is wrong on two counts:

Agreed with your conclusions

@MarcoGorelli
Copy link
Member Author

Not sure it should depend on whether or not format was passed - what I'm leaning towards is:

  • skip over any NaT-y (None, NaT, nan, 'nan', etc.) -> convert all to NaT
  • skip (or rather, preserve) pydatetime objects
  • if format is provided, parse according to that (respecting exact). Else, guess/infer

Note that with PDEP4, it'll always behave as if format was passed (unless pandas can't guess the format)

@mroeschke
Copy link
Member

Ah okay I like those guidelines then.

@jbrockmendel
Copy link
Member

looks like the example you used is the same as from #40111, so a PR that addresses this may close that

@jorisvandenbossche
Copy link
Member

Resolved by #50242

@jorisvandenbossche jorisvandenbossche added this to the 2.0 milestone Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Needs Discussion Requires discussion from core team before further action
Projects
None yet
4 participants