Skip to content

Commit 6ef695f

Browse files
authored
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
1 parent e7d2c7a commit 6ef695f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/io/json/_table_schema.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas._libs import lib
1616
from pandas._libs.json import ujson_loads
1717
from pandas._libs.tslibs import timezones
18+
from pandas._libs.tslibs.dtypes import freq_to_period_freqstr
1819
from pandas.util._exceptions import find_stack_level
1920

2021
from pandas.core.dtypes.base import _registry as registry
@@ -34,6 +35,8 @@
3435
from pandas import DataFrame
3536
import pandas.core.common as com
3637

38+
from pandas.tseries.frequencies import to_offset
39+
3740
if TYPE_CHECKING:
3841
from pandas._typing import (
3942
DtypeObj,
@@ -207,8 +210,12 @@ def convert_json_field_to_pandas_type(field) -> str | CategoricalDtype:
207210
if field.get("tz"):
208211
return f"datetime64[ns, {field['tz']}]"
209212
elif field.get("freq"):
213+
# GH#9586 rename frequency M to ME for offsets
214+
offset = to_offset(field["freq"])
215+
freq_n, freq_name = offset.n, offset.name
216+
freq = freq_to_period_freqstr(freq_n, freq_name)
210217
# GH#47747 using datetime over period to minimize the change surface
211-
return f"period[{field['freq']}]"
218+
return f"period[{freq}]"
212219
else:
213220
return "datetime64[ns]"
214221
elif typ == "any":

pandas/tests/io/json/test_json_table_schema.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,14 @@ def test_read_json_orient_table_old_schema_version(self):
845845
expected = DataFrame({"a": [1, 2.0, "s"]})
846846
result = pd.read_json(StringIO(df_json), orient="table")
847847
tm.assert_frame_equal(expected, result)
848+
849+
@pytest.mark.parametrize("freq", ["M", "2M"])
850+
def test_read_json_table_orient_period_depr_freq(self, freq, recwarn):
851+
# GH#9586
852+
df = DataFrame(
853+
{"ints": [1, 2]},
854+
index=pd.PeriodIndex(["2020-01", "2020-06"], freq=freq),
855+
)
856+
out = df.to_json(orient="table")
857+
result = pd.read_json(out, orient="table")
858+
tm.assert_frame_equal(df, result)

0 commit comments

Comments
 (0)