Skip to content

Commit

Permalink
BUG: conversion a JSON field descriptor into pandas type for deprecat…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
natmokval authored Oct 24, 2023
1 parent e7d2c7a commit 6ef695f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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)

0 comments on commit 6ef695f

Please sign in to comment.