Skip to content

Commit bbcda98

Browse files
authored
TST: Add regression tests for fixed issues (#31161)
* TST: Add tests for fixed issues * Platform compat test * Use range instead * Address comments
1 parent cdffa43 commit bbcda98

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

pandas/tests/frame/test_constructors.py

+18
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,24 @@ def test_datetime_date_tuple_columns_from_dict(self):
24332433
expected = DataFrame([0, 1, 2], columns=pd.Index(pd.Series([tup])))
24342434
tm.assert_frame_equal(result, expected)
24352435

2436+
def test_construct_with_two_categoricalindex_series(self):
2437+
# GH 14600
2438+
s1 = pd.Series(
2439+
[39, 6, 4], index=pd.CategoricalIndex(["female", "male", "unknown"])
2440+
)
2441+
s2 = pd.Series(
2442+
[2, 152, 2, 242, 150],
2443+
index=pd.CategoricalIndex(["f", "female", "m", "male", "unknown"]),
2444+
)
2445+
result = pd.DataFrame([s1, s2])
2446+
expected = pd.DataFrame(
2447+
np.array(
2448+
[[np.nan, 39.0, np.nan, 6.0, 4.0], [2.0, 152.0, 2.0, 242.0, 150.0]]
2449+
),
2450+
columns=["f", "female", "m", "male", "unknown"],
2451+
)
2452+
tm.assert_frame_equal(result, expected)
2453+
24362454

24372455
class TestDataFrameConstructorWithDatetimeTZ:
24382456
def test_from_dict(self):

pandas/tests/groupby/test_apply.py

+19
Original file line numberDiff line numberDiff line change
@@ -792,3 +792,22 @@ def test_apply_multi_level_name(category):
792792
)
793793
tm.assert_frame_equal(result, expected)
794794
assert df.index.names == ["A", "B"]
795+
796+
797+
def test_groupby_apply_datetime_result_dtypes():
798+
# GH 14849
799+
data = pd.DataFrame.from_records(
800+
[
801+
(pd.Timestamp(2016, 1, 1), "red", "dark", 1, "8"),
802+
(pd.Timestamp(2015, 1, 1), "green", "stormy", 2, "9"),
803+
(pd.Timestamp(2014, 1, 1), "blue", "bright", 3, "10"),
804+
(pd.Timestamp(2013, 1, 1), "blue", "calm", 4, "potato"),
805+
],
806+
columns=["observation", "color", "mood", "intensity", "score"],
807+
)
808+
result = data.groupby("color").apply(lambda g: g.iloc[0]).dtypes
809+
expected = Series(
810+
[np.dtype("datetime64[ns]"), np.object, np.object, np.int64, np.object],
811+
index=["observation", "color", "mood", "intensity", "score"],
812+
)
813+
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,13 @@ def test_shift_bfill_ffill_tz(tz_naive_fixture, op, expected):
19521952
tm.assert_frame_equal(result, expected)
19531953

19541954

1955+
def test_ffill_missing_arguments():
1956+
# GH 14955
1957+
df = pd.DataFrame({"a": [1, 2], "b": [1, 1]})
1958+
with pytest.raises(ValueError, match="Must specify a fill"):
1959+
df.groupby("b").fillna()
1960+
1961+
19551962
def test_groupby_only_none_group():
19561963
# see GH21624
19571964
# this was crashing with "ValueError: Length of passed values is 1, index implies 0"

pandas/tests/indexing/test_loc.py

+10
Original file line numberDiff line numberDiff line change
@@ -1002,3 +1002,13 @@ def test_loc_axis_1_slice():
10021002
),
10031003
)
10041004
tm.assert_frame_equal(result, expected)
1005+
1006+
1007+
def test_loc_set_dataframe_multiindex():
1008+
# GH 14592
1009+
expected = pd.DataFrame(
1010+
"a", index=range(2), columns=pd.MultiIndex.from_product([range(2), range(2)])
1011+
)
1012+
result = expected.copy()
1013+
result.loc[0, [(0, 1)]] = result.loc[0, [(0, 1)]]
1014+
tm.assert_frame_equal(result, expected)

pandas/tests/io/test_pickle.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
3. Move the created pickle to "data/legacy_pickle/<version>" directory.
1212
"""
1313
import bz2
14+
import datetime
1415
import glob
1516
import gzip
1617
import os
@@ -487,3 +488,17 @@ def open(self, *args):
487488
df.to_pickle(mockurl)
488489
result = pd.read_pickle(mockurl)
489490
tm.assert_frame_equal(df, result)
491+
492+
493+
class MyTz(datetime.tzinfo):
494+
def __init__(self):
495+
pass
496+
497+
498+
def test_read_pickle_with_subclass():
499+
# GH 12163
500+
expected = pd.Series(dtype=object), MyTz()
501+
result = tm.round_trip_pickle(expected)
502+
503+
tm.assert_series_equal(result[0], expected[0])
504+
assert isinstance(result[1], MyTz)

pandas/tests/series/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,15 @@ def create_data(constructor):
11151115
tm.assert_series_equal(result_datetime, expected)
11161116
tm.assert_series_equal(result_Timestamp, expected)
11171117

1118+
def test_contructor_dict_tuple_indexer(self):
1119+
# GH 12948
1120+
data = {(1, 1, None): -1.0}
1121+
result = Series(data)
1122+
expected = Series(
1123+
-1.0, index=MultiIndex(levels=[[1], [1], [np.nan]], codes=[[0], [0], [-1]])
1124+
)
1125+
tm.assert_series_equal(result, expected)
1126+
11181127
def test_constructor_mapping(self, non_mapping_dict_subclass):
11191128
# GH 29788
11201129
ndm = non_mapping_dict_subclass({3: "three"})

pandas/tests/test_multilevel.py

+34
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,40 @@ def test_sort_index_level_mixed(self):
21472147
sorted_after.drop([("foo", "three")], axis=1),
21482148
)
21492149

2150+
def test_sort_index_categorical_multiindex(self):
2151+
# GH 15058
2152+
df = DataFrame(
2153+
{
2154+
"a": range(6),
2155+
"l1": pd.Categorical(
2156+
["a", "a", "b", "b", "c", "c"],
2157+
categories=["c", "a", "b"],
2158+
ordered=True,
2159+
),
2160+
"l2": [0, 1, 0, 1, 0, 1],
2161+
}
2162+
)
2163+
result = df.set_index(["l1", "l2"]).sort_index()
2164+
expected = DataFrame(
2165+
[4, 5, 0, 1, 2, 3],
2166+
columns=["a"],
2167+
index=MultiIndex(
2168+
levels=[
2169+
pd.CategoricalIndex(
2170+
["c", "a", "b"],
2171+
categories=["c", "a", "b"],
2172+
ordered=True,
2173+
name="l1",
2174+
dtype="category",
2175+
),
2176+
[0, 1],
2177+
],
2178+
codes=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],
2179+
names=["l1", "l2"],
2180+
),
2181+
)
2182+
tm.assert_frame_equal(result, expected)
2183+
21502184
def test_is_lexsorted(self):
21512185
levels = [[0, 1], [0, 1, 2]]
21522186

0 commit comments

Comments
 (0)