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 2 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
3 changes: 3 additions & 0 deletions 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 Down Expand Up @@ -207,6 +208,8 @@ 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
field["freq"] = freq_to_period_freqstr(1, field["freq"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if field['freq'] is '2M'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, you are right, it doesn't work for '2M'. I corrected the definition of convert_json_field_to_pandas_type and updated my test.

# GH#47747 using datetime over period to minimize the change surface
return f"period[{field['freq']}]"
else:
Expand Down
25 changes: 25 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,28 @@ 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(
"index_nm",
[None, "idx", pytest.param("index", marks=pytest.mark.xfail), "level_0"],
)
@pytest.mark.parametrize(
"vals",
[
{"ints": [1, 2]},
{"objects": ["a", "b"]},
{"objects": ["1", "2"]},
{"date_ranges": pd.date_range("2016-01-01", freq="d", periods=2)},
{"floats": [1.0, 2.0]},
{"bools": [True, False]},
],
)
def test_read_json_table_orient_period_depr_freq(self, index_nm, vals, recwarn):
# GH#9586
df = DataFrame(
vals,
index=pd.Index((pd.Period("2022-01"), pd.Period("2022-04")), name=index_nm),
)
out = df.to_json(orient="table")
result = pd.read_json(out, orient="table")
tm.assert_frame_equal(df, result)