Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 24e5d75

Browse filesBrowse files
committed
BUG: fix to_json on period
1 parent b69a2ae commit 24e5d75
Copy full SHA for 24e5d75

File tree

Expand file treeCollapse file tree

4 files changed

+43
-4
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+43
-4
lines changed

‎doc/source/whatsnew/v3.0.0.rst

Copy file name to clipboardExpand all lines: doc/source/whatsnew/v3.0.0.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ I/O
750750
- Bug in :meth:`read_stata` where the missing code for double was not recognised for format versions 105 and prior (:issue:`58149`)
751751
- Bug in :meth:`set_option` where setting the pandas option ``display.html.use_mathjax`` to ``False`` has no effect (:issue:`59884`)
752752
- Bug in :meth:`to_excel` where :class:`MultiIndex` columns would be merged to a single row when ``merge_cells=False`` is passed (:issue:`60274`)
753+
- Bug in :meth:`to_json` period dtype was not being converted to string (:issue:`55490`)
753754

754755
Period
755756
^^^^^^

‎pandas/_libs/tslibs/offsets.pyx

Copy file name to clipboardExpand all lines: pandas/_libs/tslibs/offsets.pyx
+17-4Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,19 @@ cdef class BaseOffset:
568568
out += ": " + ", ".join(attrs)
569569
return out
570570

571+
def toDict(self) -> dict:
572+
"""
573+
Convert BaseOffset object to a dictionary representation
574+
and used for JSON serialization.
575+
"""
576+
d = {}
577+
# Add all attributes defined in _attributes
578+
for attr in self._attributes:
579+
if hasattr(self, attr):
580+
d[attr] = getattr(self, attr)
581+
582+
return d
583+
571584
@property
572585
def name(self) -> str:
573586
"""
@@ -5108,8 +5121,8 @@ def _warn_about_deprecated_aliases(name: str, is_period: bool) -> str:
51085121
warnings.warn(
51095122
f"\'{name}\' is deprecated and will be removed "
51105123
f"in a future version, please use "
5111-
f"\'{c_PERIOD_AND_OFFSET_DEPR_FREQSTR.get(name)}\'"
5112-
f" instead.",
5124+
f"\'{c_PERIOD_AND_OFFSET_DEPR_FREQSTR.get(name)}\' "
5125+
f"instead.",
51135126
FutureWarning,
51145127
stacklevel=find_stack_level(),
51155128
)
@@ -5122,8 +5135,8 @@ def _warn_about_deprecated_aliases(name: str, is_period: bool) -> str:
51225135
warnings.warn(
51235136
f"\'{name}\' is deprecated and will be removed "
51245137
f"in a future version, please use "
5125-
f"\'{_name}\'"
5126-
f" instead.",
5138+
f"\'{_name}\' "
5139+
f"instead.",
51275140
FutureWarning,
51285141
stacklevel=find_stack_level(),
51295142
)

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

Copy file name to clipboardExpand all lines: pandas/tests/io/json/test_pandas.py
+16Lines changed: 16 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: pandas/tests/tseries/offsets/test_offsets.py
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.