Skip to content

BUG: Fix to_json when converting Period column #32665

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

Closed
Closed
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ I/O
- Bug in :meth:`DataFrame.to_sql` where an ``AttributeError`` was raised when saving an out of bounds date (:issue:`26761`)
- Bug in :meth:`read_excel` did not correctly handle multiple embedded spaces in OpenDocument text cells. (:issue:`32207`)
- Bug in :meth:`read_json` was raising ``TypeError`` when reading a list of booleans into a Series. (:issue:`31464`)
- Bug in :meth:`to_json` was raising ``AttributeError`` with column or Series of `PeriodDtype` (:issue:`31917`)

Plotting
^^^^^^^^
Expand Down
9 changes: 9 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ def _create_series(index):
}


@pytest.fixture
def period_series():
Copy link
Member

Choose a reason for hiding this comment

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

@WillAyd did we settle on a naming scheme for these?

Copy link
Member

Choose a reason for hiding this comment

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

Seems reasonable; we already have a datetime_series so this follows that pattern

"""Fixture for Series with Period-type index.
"""
s = tm.makePeriodSeries()
s.name = 'ps'
return s


@pytest.fixture
def series_with_simple_index(indices):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def convert_pandas_type_to_json_field(arr):
field["constraints"] = {"enum": list(cats)}
field["ordered"] = ordered
elif is_period_dtype(arr):
field["freq"] = arr.freqstr
field["freq"] = arr.dtype.freq.freqstr
elif is_datetime64tz_dtype(arr):
if hasattr(arr, "dt"):
field["tz"] = arr.dt.tz.zone
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,21 @@ def test_series_roundtrip_timeseries(self, orient, numpy, datetime_series):

tm.assert_series_equal(result, expected)

def test_series_roundtrip_periodseries(self, orient, period_series):
# GH32665: Fix to_json when converting Period column/series
data = period_series.to_json(orient=orient)
result = pd.read_json(data, typ="series", orient=orient)
Copy link
Member

Choose a reason for hiding this comment

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

This issue affects reading to a DataFrame as well right? If so can you test that?

Copy link
Author

Choose a reason for hiding this comment

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

OK, I can do that.


expected = period_series
if orient in ("values", "records"):
expected = expected.reset_index(drop=True)
if orient in ("index", "columns"):
result.index = result.index.to_period()
if orient != "split":
expected.name = None

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("dtype", [np.float64, np.int])
@pytest.mark.parametrize("numpy", [True, False])
def test_series_roundtrip_numeric(self, orient, numpy, dtype):
Expand Down