Skip to content
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

BUG: fix to_json on period #61203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xiaohuanlin
Copy link
Contributor

@xiaohuanlin xiaohuanlin commented Mar 30, 2025

@xiaohuanlin xiaohuanlin changed the title (WIP)BUG: fix to_json on period BUG: fix to_json on period Mar 30, 2025
@@ -568,6 +568,19 @@ cdef class BaseOffset:
out += ": " + ", ".join(attrs)
return out

def toDict(self) -> dict:
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer not to implement this here but rather in the C extension code itself; this way of making something JSON serializable takes advantage of an obscure ultrajson method of allowing custom objects to be serialized, and it will be pretty slow compared to direct implementation in our C extension

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree with you on implementing it in C extension code

# GH55490
ser = Series(pd.period_range(start=2021, freq="Y", periods=1))
result = ser.to_json()
expected = (
Copy link
Member

Choose a reason for hiding this comment

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

Is this really the JSON representation that users want for a period?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found it interesting that the Period class actually has a freqstr property. This allows us to ignore freq when running to_json, which makes sense since freqstr already contains the necessary frequency information. Additionally, this approach helps resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: .to_json() of periode_range gives OverflowError: Maximum recursion level reached
2 participants