Skip to content

TST: Add regression tests for old issues #41389

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

Merged
merged 9 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,18 @@ def test_integer_array_add_list_like(

assert_function(left, expected)
assert_function(right, expected)


def test_sub_multiindex_swapped_levels():
# GH 9952
df = pd.DataFrame(
{"a": np.random.randn(6)},
index=pd.MultiIndex.from_product(
[["a", "b"], [0, 1, 2]], names=["levA", "levB"]
),
)
df2 = df.copy()
df2.index = df2.index.swaplevel(0, 1)
result = df - df2
expected = pd.DataFrame([0.0] * 6, columns=["a"], index=df.index)
tm.assert_frame_equal(result, expected)
16 changes: 16 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,19 @@ def test_where_string_dtype(frame_or_series):
dtype=StringDtype(),
)
tm.assert_equal(result, expected)


def test_where_bool_comparison():
# GH 10336
df_mask = DataFrame(
{"AAA": [True] * 4, "BBB": [False] * 4, "CCC": [True, False, True, False]}
)
result = df_mask.where(df_mask == False) # noqa:E712
expected = DataFrame(
{
"AAA": np.array([np.nan] * 4, dtype=object),
"BBB": [False] * 4,
"CCC": [np.nan, False, np.nan, False],
}
)
tm.assert_frame_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,17 @@ def test_reset_index_empty_frame_with_datetime64_multiindex_from_groupby():
expected["c3"] = expected["c3"].astype("datetime64[ns]")
expected["c1"] = expected["c1"].astype("float64")
tm.assert_frame_equal(result, expected)


def test_reset_index_multiindex_nat():
# GH 11479
idx = range(3)
tstamp = date_range("2015-07-01", freq="D", periods=3)
df = DataFrame({"id": idx, "tstamp": tstamp, "a": list("abc")})
df.loc[2, "tstamp"] = pd.NaT
result = df.set_index(["id", "tstamp"]).reset_index("id")
expected = DataFrame(
{"id": range(3), "a": list("abc")},
index=pd.DatetimeIndex(["2015-07-01", "2015-07-02", "NaT"], name="tstamp"),
)
tm.assert_frame_equal(result, expected)
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,12 @@ def check_views():
# assert b[0] == 0
assert df.iloc[0, 2] == 0

def test_from_series_with_name_with_columns(self):
# GH 7893
result = DataFrame(Series(1, name="foo"), columns=["bar"])
expected = DataFrame(columns=["bar"])
tm.assert_frame_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

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

this strikes me as weird. if the series's name was "bar", then result would have length 1 instead of 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I'm not sure; just seems it was the preferred behavior from #7893 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

cc @jreback as you had mentioned that this behavior was expected

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah we should prob deprecate this but its the current behavior



class TestDataFrameConstructorWithDatetimeTZ:
@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,3 +1998,23 @@ def test_stack_nan_in_multiindex_columns(self):
columns=Index([(0, None), (0, 2), (0, 3)]),
)
tm.assert_frame_equal(result, expected)

def test_stack_nan_level(self):
# GH 9406
df_nan = DataFrame(
np.arange(4).reshape(2, 2),
columns=MultiIndex.from_tuples(
[("A", np.nan), ("B", "b")], names=["Upper", "Lower"]
),
index=Index([0, 1], name="Num"),
dtype=np.float64,
)
result = df_nan.stack()
expected = DataFrame(
[[0.0, np.nan], [np.nan, 1], [2.0, np.nan], [np.nan, 3.0]],
columns=Index(["A", "B"], name="Upper"),
index=MultiIndex.from_tuples(
[(0, np.nan), (0, "b"), (1, np.nan), (1, "b")], names=["Num", "Lower"]
),
)
tm.assert_frame_equal(result, expected)
12 changes: 12 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,15 @@ def test_loc_getitem_drops_levels_for_one_row_dataframe():
result = ser.loc["x", :, "z"]
expected = Series([0], index=Index(["y"], name="b"))
tm.assert_series_equal(result, expected)


def test_mi_columns_loc_list_label_order():
# GH 10710
cols = MultiIndex.from_product([["A", "B", "C"], [1, 2]])
df = DataFrame(np.zeros((5, 6)), columns=cols)
result = df.loc[:, ["B", "A"]]
expected = DataFrame(
np.zeros((5, 4)),
columns=MultiIndex.from_tuples([("B", 1), ("B", 2), ("A", 1), ("A", 2)]),
)
tm.assert_frame_equal(result, expected)
8 changes: 8 additions & 0 deletions pandas/tests/reshape/concat/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ def test_concat_tz_NaT(self, t1):

tm.assert_frame_equal(result, expected)

def test_concat_tz_with_empty(self):
# GH 9188
result = concat(
[DataFrame(date_range("2000", periods=1, tz="UTC")), DataFrame()]
)
expected = DataFrame(date_range("2000", periods=1, tz="UTC"))
tm.assert_frame_equal(result, expected)


class TestPeriodConcat:
def test_concat_period_series(self):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ def test_setitem_slicestep(self):
series[::2] = 0
assert (series[::2] == 0).all()

def test_setitem_multiindex_slice(self, indexer_sli):
# GH 8856
mi = MultiIndex.from_product(([0, 1], list("abcde")))
result = Series(np.arange(10, dtype=np.int64), mi)
indexer_sli(result)[::4] = 100
expected = Series([100, 1, 2, 3, 100, 5, 6, 7, 100, 9], mi)
tm.assert_series_equal(result, expected)


class TestSetitemBooleanMask:
def test_setitem_boolean(self, string_series):
Expand Down