From 6ef695ff19cbc54c7f17f55aedb79e2757784670 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva <91160475+natmokval@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:41:43 +0200 Subject: [PATCH] BUG: conversion a JSON field descriptor into pandas type for deprecated offsets frequency 'M' (#55581) * correct convert_json_field_to_pandas_type * fix pre-commit errors * correct def convert_json_field_to_pandas_type in case freq_n>1 * remove print * add parameterization to the test * replace the regex with to_offset --- pandas/io/json/_table_schema.py | 9 ++++++++- pandas/tests/io/json/test_json_table_schema.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pandas/io/json/_table_schema.py b/pandas/io/json/_table_schema.py index 3f2291ba7a0c3..4d9fba72cf173 100644 --- a/pandas/io/json/_table_schema.py +++ b/pandas/io/json/_table_schema.py @@ -15,6 +15,7 @@ from pandas._libs import lib from pandas._libs.json import ujson_loads from pandas._libs.tslibs import timezones +from pandas._libs.tslibs.dtypes import freq_to_period_freqstr from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import _registry as registry @@ -34,6 +35,8 @@ from pandas import DataFrame import pandas.core.common as com +from pandas.tseries.frequencies import to_offset + if TYPE_CHECKING: from pandas._typing import ( DtypeObj, @@ -207,8 +210,12 @@ def convert_json_field_to_pandas_type(field) -> str | CategoricalDtype: if field.get("tz"): return f"datetime64[ns, {field['tz']}]" elif field.get("freq"): + # GH#9586 rename frequency M to ME for offsets + offset = to_offset(field["freq"]) + freq_n, freq_name = offset.n, offset.name + freq = freq_to_period_freqstr(freq_n, freq_name) # GH#47747 using datetime over period to minimize the change surface - return f"period[{field['freq']}]" + return f"period[{freq}]" else: return "datetime64[ns]" elif typ == "any": diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index 943515acd33b5..ddab3887db810 100644 --- a/pandas/tests/io/json/test_json_table_schema.py +++ b/pandas/tests/io/json/test_json_table_schema.py @@ -845,3 +845,14 @@ def test_read_json_orient_table_old_schema_version(self): expected = DataFrame({"a": [1, 2.0, "s"]}) result = pd.read_json(StringIO(df_json), orient="table") tm.assert_frame_equal(expected, result) + + @pytest.mark.parametrize("freq", ["M", "2M"]) + def test_read_json_table_orient_period_depr_freq(self, freq, recwarn): + # GH#9586 + df = DataFrame( + {"ints": [1, 2]}, + index=pd.PeriodIndex(["2020-01", "2020-06"], freq=freq), + ) + out = df.to_json(orient="table") + result = pd.read_json(out, orient="table") + tm.assert_frame_equal(df, result)