Skip to content

BUG: conversion a JSON field descriptor into pandas type for deprecated offsets frequency 'M' #55581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)