diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 55b987a599670..f7b49ccb1a72d 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -258,6 +258,36 @@ def test_read_excel_parse_dates(self, ext): ) tm.assert_frame_equal(df, res) + def test_multiindex_interval_datetimes(self, ext): + # GH 30986 + midx = pd.MultiIndex.from_arrays( + [ + range(4), + pd.interval_range( + start=pd.Timestamp("2020-01-01"), periods=4, freq="6M" + ), + ] + ) + df = pd.DataFrame(range(4), index=midx) + with tm.ensure_clean(ext) as pth: + df.to_excel(pth) + result = pd.read_excel(pth, index_col=[0, 1]) + expected = pd.DataFrame( + range(4), + pd.MultiIndex.from_arrays( + [ + range(4), + [ + "(2020-01-31, 2020-07-31]", + "(2020-07-31, 2021-01-31]", + "(2021-01-31, 2021-07-31]", + "(2021-07-31, 2022-01-31]", + ], + ] + ), + ) + tm.assert_frame_equal(result, expected) + @td.skip_if_no("xlrd") @pytest.mark.parametrize( diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index efb95a0cb2a42..91b204ed41ebc 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -3,7 +3,7 @@ import numpy as np import pytest -from pandas import DataFrame, Index, json_normalize +from pandas import DataFrame, Index, Series, json_normalize import pandas._testing as tm from pandas.io.json._normalize import nested_to_record @@ -728,3 +728,24 @@ def test_deprecated_import(self): recs = [{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}] json_normalize(recs) + + def test_series_non_zero_index(self): + # GH 19020 + data = { + 0: {"id": 1, "name": "Foo", "elements": {"a": 1}}, + 1: {"id": 2, "name": "Bar", "elements": {"b": 2}}, + 2: {"id": 3, "name": "Baz", "elements": {"c": 3}}, + } + s = Series(data) + s.index = [1, 2, 3] + result = json_normalize(s) + expected = DataFrame( + { + "id": [1, 2, 3], + "name": ["Foo", "Bar", "Baz"], + "elements.a": [1.0, np.nan, np.nan], + "elements.b": [np.nan, 2.0, np.nan], + "elements.c": [np.nan, np.nan, 3.0], + } + ) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 955f8c7482937..ff303b808f6f5 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -870,3 +870,15 @@ def test_get_period_range_edges(self, first, last, offset, exp_first, exp_last): result = _get_period_range_edges(first, last, offset) expected = (exp_first, exp_last) assert result == expected + + def test_sum_min_count(self): + # GH 19974 + index = pd.date_range(start="2018", freq="M", periods=6) + data = np.ones(6) + data[3:6] = np.nan + s = pd.Series(data, index).to_period() + result = s.resample("Q").sum(min_count=1) + expected = pd.Series( + [3.0, np.nan], index=PeriodIndex(["2018Q1", "2018Q2"], freq="Q-DEC") + ) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index ed9d0cffe2304..640cd8faf6811 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -891,6 +891,31 @@ def manual_compare_stacked(df, df_stacked, lev0, lev1): ) manual_compare_stacked(df, df.stack(0), 0, 1) + def test_stack_unstack_unordered_multiindex(self): + # GH 18265 + values = np.arange(5) + data = np.vstack( + [ + ["b{}".format(x) for x in values], # b0, b1, .. + ["a{}".format(x) for x in values], + ] + ) # a0, a1, .. + df = pd.DataFrame(data.T, columns=["b", "a"]) + df.columns.name = "first" + second_level_dict = {"x": df} + multi_level_df = pd.concat(second_level_dict, axis=1) + multi_level_df.columns.names = ["second", "first"] + df = multi_level_df.reindex(sorted(multi_level_df.columns), axis=1) + result = df.stack(["first", "second"]).unstack(["first", "second"]) + expected = DataFrame( + [["a0", "b0"], ["a1", "b1"], ["a2", "b2"], ["a3", "b3"], ["a4", "b4"]], + index=[0, 1, 2, 3, 4], + columns=MultiIndex.from_tuples( + [("a", "x"), ("b", "x")], names=["first", "second"] + ), + ) + tm.assert_frame_equal(result, expected) + def test_groupby_corner(self): midx = MultiIndex( levels=[["foo"], ["bar"], ["baz"]],