From 70c7e7fa026a91705267e753bde616c99ea64497 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Thu, 14 Nov 2024 19:20:43 +0800 Subject: [PATCH 1/2] catch and supress "to_datetime" FutureWarning deprecation --- pandas/io/json/_json.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 9414f45215029..e0e42880075e5 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -1210,7 +1210,7 @@ def _convert_axes(self) -> None: name=axis_name, data=ser, use_dtypes=False, - convert_dates=True, + convert_dates=self.convert_dates, is_axis=True, ) if result: @@ -1357,6 +1357,12 @@ def _try_convert_to_date(self, data: Series) -> tuple[Series, bool]: "zones will raise an error", category=FutureWarning, ) + warnings.filterwarnings( + "ignore", + "The behavior of 'to_datetime' with 'unit' " + "when parsing strings is deprecated.*", + category=FutureWarning, + ) new_data = to_datetime(new_data, errors="raise", unit=date_unit) except (ValueError, OverflowError, TypeError): continue From b0315f3be635ec464eb6b7adf759fe5ceb35fe30 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Thu, 14 Nov 2024 19:22:24 +0800 Subject: [PATCH 2/2] add tests --- pandas/tests/io/json/test_pandas.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index a8608434be5ee..b0e72f2d0099f 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -2192,3 +2192,9 @@ def test_read_json_lines_rangeindex(): result = read_json(StringIO(data), lines=True).index expected = RangeIndex(2) tm.assert_index_equal(result, expected, exact=True) + + +def test_read_json_to_datetime_futurewarning_supress(): + data = '{"A":{"0":"X","Y":"Y"}}' + with tm.assert_produces_warning(None): + read_json(StringIO(data))