-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
colonesej
wants to merge
17
commits into
pandas-dev:master
from
colonesej:to_json-period-type-column
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8f8c450
BUG: Fix to_json when converting Period column
colonesej 527e2ef
Update v1.1.0.rst
colonesej 2e7f556
Merge branch 'master' into to_json-period-type-column
colonesej beb50ae
Merge branch 'master' into to_json-period-type-column
colonesej 8c365dd
Merge branch 'master' into to_json-period-type-column
colonesej a7b2310
first attempt rountrip tests for to_json on period series
colonesej 7d4ea90
Merge branch 'to_json-period-type-column' of https://github.com/colon…
colonesej 2e674a0
Update conftest.py
colonesej 67f0f4f
Update test_pandas.py
colonesej 25287b6
Update v1.1.0.rst
colonesej f42000f
Merge branch 'master' into to_json-period-type-column
colonesej 38ea13b
to_json roundtrip tests with frames with periodDtype index and column
colonesej 68102cf
explicitly giving frequency before comparing roundtrip tests on Perio…
colonesej 45e4c76
master 10/06/2002
colonesej 09cf5f5
pep8 corrections
colonesej 223380d
black reformatting
colonesej 8bdddcf
Merge branch 'master' into to_json-period-type-column
colonesej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,3 +118,4 @@ doc/build/html/index.html | |
doc/tmp.sv | ||
env/ | ||
doc/source/savefig/ | ||
venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,23 +42,6 @@ def setup(self): | |
|
||
yield | ||
|
||
@pytest.fixture | ||
def datetime_series(self): | ||
# Same as usual datetime_series, but with index freq set to None, | ||
# since that doesnt round-trip, see GH#33711 | ||
ser = tm.makeTimeSeries() | ||
ser.name = "ts" | ||
ser.index = ser.index._with_freq(None) | ||
return ser | ||
|
||
@pytest.fixture | ||
def datetime_frame(self): | ||
# Same as usual datetime_frame, but with index freq set to None, | ||
# since that doesnt round-trip, see GH#33711 | ||
df = DataFrame(tm.getTimeSeriesData()) | ||
df.index = df.index._with_freq(None) | ||
return df | ||
|
||
def test_frame_double_encoded_labels(self, orient): | ||
df = DataFrame( | ||
[["a", "b"], ["c", "d"]], | ||
|
@@ -382,6 +365,42 @@ def test_frame_to_json_except(self): | |
with pytest.raises(ValueError, match=msg): | ||
df.to_json(orient="garbage") | ||
|
||
def test_frame_roundtrip_period_index(self, orient, period_frame): | ||
# GH32665: Fix to_json when converting Period column/series | ||
if orient == "split": | ||
pytest.skip("skipping orient=split due to different conversion schema") | ||
|
||
data = period_frame.to_json(orient=orient) | ||
result = pd.read_json(data, typ="frame", orient=orient) | ||
|
||
expected = period_frame.copy() | ||
if orient in ("values", "records"): | ||
expected = expected.reset_index(drop=True) | ||
if orient == "values": | ||
# drop column names as well | ||
expected = expected.T.reset_index(drop=True).T | ||
if orient in ("index", "columns"): | ||
result.index = result.index.to_period(freq=expected.index.freq) | ||
expected.name = None | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.skip(reason="Conversion of Period-like column in dict-like format") | ||
def test_frame_roundtrip_period_columns(self, orient, period_frame): | ||
# GH32665: Fix to_json when converting Period column/series | ||
|
||
test_frame = period_frame.reset_index() | ||
data = test_frame.to_json(orient=orient) | ||
result = pd.read_json(data, typ="frame", orient=orient) | ||
|
||
expected = test_frame | ||
if orient == "values": | ||
expected.colummns = range(len(expected.columns)) | ||
if orient != "split": | ||
expected.name = None | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_frame_empty(self): | ||
df = DataFrame(columns=["jim", "joe"]) | ||
assert not df._is_mixed_type | ||
|
@@ -433,9 +452,6 @@ def test_frame_mixedtype_orient(self): # GH10289 | |
tm.assert_frame_equal(left, right) | ||
|
||
def test_v12_compat(self, datapath): | ||
dti = pd.date_range("2000-01-03", "2000-01-07") | ||
# freq doesnt roundtrip | ||
dti = pd.DatetimeIndex(np.asarray(dti), freq=None) | ||
df = DataFrame( | ||
[ | ||
[1.56808523, 0.65727391, 1.81021139, -0.17251653], | ||
|
@@ -445,7 +461,7 @@ def test_v12_compat(self, datapath): | |
[0.05951614, -2.69652057, 1.28163262, 0.34703478], | ||
], | ||
columns=["A", "B", "C", "D"], | ||
index=dti, | ||
index=pd.date_range("2000-01-03", "2000-01-07"), | ||
) | ||
df["date"] = pd.Timestamp("19920106 18:21:32.12") | ||
df.iloc[3, df.columns.get_loc("date")] = pd.Timestamp("20130101") | ||
|
@@ -464,9 +480,6 @@ def test_v12_compat(self, datapath): | |
|
||
def test_blocks_compat_GH9037(self): | ||
index = pd.date_range("20000101", periods=10, freq="H") | ||
# freq doesnt round-trip | ||
index = pd.DatetimeIndex(list(index), freq=None) | ||
|
||
df_mixed = DataFrame( | ||
OrderedDict( | ||
float_1=[ | ||
|
@@ -673,6 +686,23 @@ 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 | ||
if orient == "split": | ||
pytest.skip("skipping orient=split due to different conversion schema") | ||
|
||
data = period_series.to_json(orient=orient) | ||
result = pd.read_json(data, typ="series", orient=orient) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(freq=expected.index.freq) | ||
expected.name = None | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("dtype", [np.float64, int]) | ||
@pytest.mark.parametrize("numpy", [True, False]) | ||
def test_series_roundtrip_numeric(self, orient, numpy, dtype): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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