Skip to content

Commit aaecadb

Browse files
mroeschkeTLouf
authored andcommitted
TST: More old issues (pandas-dev#41697)
1 parent 5719d73 commit aaecadb

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

pandas/tests/frame/indexing/test_setitem.py

+12
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,18 @@ def test_setitem_iloc_two_dimensional_generator(self):
626626
expected = DataFrame({"a": [1, 2, 3], "b": [4, 1, 1]})
627627
tm.assert_frame_equal(df, expected)
628628

629+
def test_setitem_dtypes_bytes_type_to_object(self):
630+
# GH 20734
631+
index = Series(name="id", dtype="S24")
632+
df = DataFrame(index=index)
633+
df["a"] = Series(name="a", index=index, dtype=np.uint32)
634+
df["b"] = Series(name="b", index=index, dtype="S64")
635+
df["c"] = Series(name="c", index=index, dtype="S64")
636+
df["d"] = Series(name="d", index=index, dtype=np.uint8)
637+
result = df.dtypes
638+
expected = Series([np.uint32, object, object, np.uint8], index=list("abcd"))
639+
tm.assert_series_equal(result, expected)
640+
629641

630642
class TestSetitemTZAwareValues:
631643
@pytest.fixture

pandas/tests/groupby/test_apply.py

+32
Original file line numberDiff line numberDiff line change
@@ -1146,3 +1146,35 @@ def test_apply_as_index_constant_lambda(as_index, expected):
11461146
df = DataFrame({"a": [1, 1, 2, 2], "b": [1, 1, 2, 2], "c": [1, 1, 1, 1]})
11471147
result = df.groupby(["a", "b"], as_index=as_index).apply(lambda x: 1)
11481148
tm.assert_equal(result, expected)
1149+
1150+
1151+
def test_sort_index_groups():
1152+
# GH 20420
1153+
df = DataFrame(
1154+
{"A": [1, 2, 3, 4, 5], "B": [6, 7, 8, 9, 0], "C": [1, 1, 1, 2, 2]},
1155+
index=range(5),
1156+
)
1157+
result = df.groupby("C").apply(lambda x: x.A.sort_index())
1158+
expected = Series(
1159+
range(1, 6),
1160+
index=MultiIndex.from_tuples(
1161+
[(1, 0), (1, 1), (1, 2), (2, 3), (2, 4)], names=["C", None]
1162+
),
1163+
name="A",
1164+
)
1165+
tm.assert_series_equal(result, expected)
1166+
1167+
1168+
def test_positional_slice_groups_datetimelike():
1169+
# GH 21651
1170+
expected = DataFrame(
1171+
{
1172+
"date": pd.date_range("2010-01-01", freq="12H", periods=5),
1173+
"vals": range(5),
1174+
"let": list("abcde"),
1175+
}
1176+
)
1177+
result = expected.groupby([expected.let, expected.date.dt.date]).apply(
1178+
lambda x: x.iloc[0:]
1179+
)
1180+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_nth.py

+22
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,25 @@ def test_nth_nan_in_grouper(dropna):
641641
)
642642

643643
tm.assert_frame_equal(result, expected)
644+
645+
646+
def test_first_categorical_and_datetime_data_nat():
647+
# GH 20520
648+
df = DataFrame(
649+
{
650+
"group": ["first", "first", "second", "third", "third"],
651+
"time": 5 * [np.datetime64("NaT")],
652+
"categories": Series(["a", "b", "c", "a", "b"], dtype="category"),
653+
}
654+
)
655+
result = df.groupby("group").first()
656+
expected = DataFrame(
657+
{
658+
"time": 3 * [np.datetime64("NaT")],
659+
"categories": Series(["a", "c", "a"]).astype(
660+
pd.CategoricalDtype(["a", "b", "c"])
661+
),
662+
}
663+
)
664+
expected.index = Index(["first", "second", "third"], name="group")
665+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/transform/test_transform.py

+8
Original file line numberDiff line numberDiff line change
@@ -1259,3 +1259,11 @@ def test_categorical_and_not_categorical_key(observed):
12591259
tm.assert_series_equal(result, expected)
12601260
expected_explicit = Series([4, 2, 4], name="B")
12611261
tm.assert_series_equal(result, expected_explicit)
1262+
1263+
1264+
def test_string_rank_grouping():
1265+
# GH 19354
1266+
df = DataFrame({"A": [1, 1, 2], "B": [1, 2, 3]})
1267+
result = df.groupby("A").transform("rank")
1268+
expected = DataFrame({"B": [1.0, 2.0, 1.0]})
1269+
tm.assert_frame_equal(result, expected)

pandas/tests/indexing/multiindex/test_loc.py

+24
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,27 @@ def test_loc_get_scalar_casting_to_float():
866866
result = df.loc[[(3, 4)], "b"].iloc[0]
867867
assert result == 2
868868
assert isinstance(result, np.int64)
869+
870+
871+
def test_loc_empty_single_selector_with_names():
872+
# GH 19517
873+
idx = MultiIndex.from_product([["a", "b"], ["A", "B"]], names=[1, 0])
874+
s2 = Series(index=idx, dtype=np.float64)
875+
result = s2.loc["a"]
876+
expected = Series([np.nan, np.nan], index=Index(["A", "B"], name=0))
877+
tm.assert_series_equal(result, expected)
878+
879+
880+
def test_loc_keyerror_rightmost_key_missing():
881+
# GH 20951
882+
883+
df = DataFrame(
884+
{
885+
"A": [100, 100, 200, 200, 300, 300],
886+
"B": [10, 10, 20, 21, 31, 33],
887+
"C": range(6),
888+
}
889+
)
890+
df = df.set_index(["A", "B"])
891+
with pytest.raises(KeyError, match="^1$"):
892+
df.loc[(100, 1)]

pandas/tests/series/test_arithmetic.py

+23
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,26 @@ def test_none_comparison(series_with_simple_index):
910910
result = series < None
911911
assert not result.iat[0]
912912
assert not result.iat[1]
913+
914+
915+
def test_series_varied_multiindex_alignment():
916+
# GH 20414
917+
s1 = Series(
918+
range(8),
919+
index=pd.MultiIndex.from_product(
920+
[list("ab"), list("xy"), [1, 2]], names=["ab", "xy", "num"]
921+
),
922+
)
923+
s2 = Series(
924+
[1000 * i for i in range(1, 5)],
925+
index=pd.MultiIndex.from_product([list("xy"), [1, 2]], names=["xy", "num"]),
926+
)
927+
result = s1.loc[pd.IndexSlice["a", :, :]] + s2
928+
expected = Series(
929+
[1000, 2001, 3002, 4003],
930+
index=pd.MultiIndex.from_tuples(
931+
[("a", "x", 1), ("a", "x", 2), ("a", "y", 1), ("a", "y", 2)],
932+
names=["ab", "xy", "num"],
933+
),
934+
)
935+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)