Skip to content

Commit 8dfcb75

Browse files
CLN: simplify logic in json date parsing regarding warnings (#66760)
1 parent 4c6a784 commit 8dfcb75

1 file changed

Lines changed: 12 additions & 26 deletions

File tree

pandas/io/json/_json.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,18 +1669,15 @@ def _try_convert_to_date(self, data: Series) -> Series:
16691669
converted = to_datetime(new_data, errors="raise", format=format)
16701670
except Exception:
16711671
pass
1672-
_reemit_parse_warnings(
1673-
record, kept=converted is not None, ignore_user_warnings=True
1674-
)
16751672
if converted is not None:
1673+
_reemit_parse_warnings(record, ignore_user_warnings=True)
16761674
return converted
16771675
else:
16781676
# numeric or mixed objects
16791677
date_units = (self.date_unit,) if self.date_unit else self._STAMP_UNITS
1680-
kept_record: list[warnings.WarningMessage] = []
1678+
converted = None
1679+
in_ns_bounds = False
16811680
for date_unit in date_units:
1682-
converted = None
1683-
in_ns_bounds = False
16841681
with warnings.catch_warnings(record=True) as record:
16851682
warnings.simplefilter("always")
16861683
try:
@@ -1700,28 +1697,23 @@ def _try_convert_to_date(self, data: Series) -> Series:
17001697
TypeError,
17011698
):
17021699
pass
1703-
if converted is None:
1704-
_reemit_parse_warnings(record, kept=False)
1705-
else:
1706-
# `data` is returned below even when the bounds check failed,
1707-
# so this attempt counts as kept either way; only the last
1708-
# such attempt survives, so defer its warnings until then
1700+
if converted is not None and in_ns_bounds:
1701+
_reemit_parse_warnings(record)
17091702
data = converted
1710-
kept_record = record
1711-
if in_ns_bounds:
17121703
break
1713-
_reemit_parse_warnings(kept_record, kept=True)
1704+
else:
1705+
if converted is not None:
1706+
# all units failed to cast to ns (eg with mixed string / int)
1707+
# but to_datetime still returned a result -> use this this
1708+
# result (with the last unit) and re-emit any warning
1709+
_reemit_parse_warnings(record)
1710+
data = converted
17141711
return data
17151712

17161713

1717-
# GH#50907; matched on the message too, since Pandas4Warning covers many deprecations
1718-
_QUARTER_DEPR_MSG = "as a quarterly string is deprecated"
1719-
1720-
17211714
def _reemit_parse_warnings(
17221715
record: list[warnings.WarningMessage],
17231716
*,
1724-
kept: bool,
17251717
ignore_user_warnings: bool = False,
17261718
) -> None:
17271719
"""
@@ -1737,12 +1729,6 @@ def _reemit_parse_warnings(
17371729
if ignore_user_warnings and issubclass(warning.category, UserWarning):
17381730
# "Could not infer format", incorrectly raised for non-date strings
17391731
continue
1740-
if (
1741-
not kept
1742-
and issubclass(warning.category, Pandas4Warning)
1743-
and _QUARTER_DEPR_MSG in str(warning.message)
1744-
):
1745-
continue
17461732
warnings.warn(
17471733
warning.message,
17481734
warning.category,

0 commit comments

Comments
 (0)