Skip to content

Commit 0eda6d9

Browse files
committed
fix json cases
1 parent 73c582c commit 0eda6d9

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pandas/io/json/_json.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,11 @@ def _try_convert_to_date(self, data: Series) -> Series:
13121312
date_units = (self.date_unit,) if self.date_unit else self._STAMP_UNITS
13131313
for date_unit in date_units:
13141314
try:
1315-
return to_datetime(new_data, errors="raise", unit=date_unit)
1315+
# Without this as_unit cast, we would fail to overflow
1316+
# and get much-too-large dates
1317+
return to_datetime(new_data, errors="raise", unit=date_unit).dt.as_unit(
1318+
"ns"
1319+
)
13161320
except (ValueError, OverflowError, TypeError):
13171321
continue
13181322
return data

pandas/tests/io/json/test_pandas.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ def test_date_format_series(self, date, date_unit, datetime_series):
964964
json = ts.to_json(date_format="iso")
965965

966966
result = read_json(StringIO(json), typ="series")
967-
expected = ts.copy()
967+
expected = ts.copy().dt.as_unit("ns")
968968
tm.assert_series_equal(result, expected)
969969

970970
def test_date_format_series_raises(self, datetime_series):
@@ -1118,9 +1118,9 @@ def test_round_trip_exception(self, datapath):
11181118
@pytest.mark.parametrize(
11191119
"field,dtype",
11201120
[
1121-
["created_at", pd.DatetimeTZDtype(tz="UTC", unit="us")],
1122-
["closed_at", "datetime64[us]"],
1123-
["updated_at", pd.DatetimeTZDtype(tz="UTC", unit="us")],
1121+
["created_at", pd.DatetimeTZDtype(tz="UTC")],
1122+
["closed_at", "datetime64[ns]"],
1123+
["updated_at", pd.DatetimeTZDtype(tz="UTC")],
11241124
],
11251125
)
11261126
def test_url(self, field, dtype, httpserver):
@@ -1756,7 +1756,7 @@ def test_read_timezone_information(self):
17561756
result = read_json(
17571757
StringIO('{"2019-01-01T11:00:00.000Z":88}'), typ="series", orient="index"
17581758
)
1759-
exp_dti = DatetimeIndex(["2019-01-01 11:00:00"], dtype="M8[us, UTC]")
1759+
exp_dti = DatetimeIndex(["2019-01-01 11:00:00"], dtype="M8[ns, UTC]")
17601760
expected = Series([88], index=exp_dti)
17611761
tm.assert_series_equal(result, expected)
17621762

0 commit comments

Comments
 (0)