Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9aba7ce

Browse files
committedMar 30, 2025·
BUG: fix to_json on period
1 parent b69a2ae commit 9aba7ce

File tree

3 files changed

+173
-509
lines changed

3 files changed

+173
-509
lines changed
 

‎pandas/_libs/tslibs/offsets.pyx

+148-509
Large diffs are not rendered by default.

‎pandas/tests/io/json/test_pandas.py

+16
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,22 @@ def e(self):
20792079
series = Series([_TestObject(a=1, b=2, _c=3, d=4)])
20802080
assert json.loads(series.to_json()) == {"0": {"a": 1, "b": 2, "d": 4}}
20812081

2082+
def test_to_json_with_period(self):
2083+
# GH55490
2084+
ser = Series(pd.period_range(start=2021, freq="Y", periods=1))
2085+
result = ser.to_json()
2086+
expected = (
2087+
'{"0":{"day":31,"day_of_week":4,"day_of_year":365, '
2088+
'"dayofweek":4,"dayofyear":365,"days_in_month":31, '
2089+
'"daysinmonth":31,"end_time":1640995199999, '
2090+
'"freq":{"n":1,"normalize":false,"month":12}, '
2091+
'"freqstr":"Y-DEC","hour":0,"is_leap_year":false, '
2092+
'"minute":0,"month":12,"ordinal":51,"quarter":4, '
2093+
'"qyear":2021,"second":0,"start_time":1609459200000, '
2094+
'"week":52,"weekday":4,"weekofyear":52,"year":2021}}'
2095+
)
2096+
assert result == expected
2097+
20822098
@pytest.mark.parametrize(
20832099
"data,expected",
20842100
[

‎pandas/tests/tseries/offsets/test_offsets.py

+9
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,12 @@ def test_is_yqm_start_end():
12271227
def test_multiply_dateoffset_typeerror(left, right):
12281228
with pytest.raises(TypeError, match="Cannot multiply"):
12291229
left * right
1230+
1231+
1232+
def test_toDict(offset_types):
1233+
offset = offset_types(n=2)
1234+
d = offset.toDict()
1235+
1236+
for attr in offset._attributes:
1237+
if hasattr(offset, attr):
1238+
assert d[attr] == getattr(offset, attr)

0 commit comments

Comments
 (0)
Please sign in to comment.