Skip to content

Commit d77b1d8

Browse files
authored
TST: Add more regression tests for fixed issues (#31171)
* TST: Add more regression tests for fixed issues * Fix lint and platform compat * Using intp * Move interval indexing test to appropriate location
1 parent 4050e4c commit d77b1d8

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

pandas/tests/frame/test_constructors.py

+6
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,12 @@ def test_constructor_dict_order_insertion(self):
412412
expected = DataFrame(data=d, columns=list("ba"))
413413
tm.assert_frame_equal(frame, expected)
414414

415+
def test_constructor_dict_nan_key_and_columns(self):
416+
# GH 16894
417+
result = pd.DataFrame({np.nan: [1, 2], 2: [2, 3]}, columns=[np.nan, 2])
418+
expected = pd.DataFrame([[1, 2], [2, 3]], columns=[np.nan, 2])
419+
tm.assert_frame_equal(result, expected)
420+
415421
def test_constructor_multi_index(self):
416422
# GH 4078
417423
# construction error with mi and all-nan frame

pandas/tests/groupby/aggregate/test_aggregate.py

+16
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ def test_lambda_named_agg(func):
630630
tm.assert_frame_equal(result, expected)
631631

632632

633+
def test_aggregate_mixed_types():
634+
# GH 16916
635+
df = pd.DataFrame(
636+
data=np.array([0] * 9).reshape(3, 3), columns=list("XYZ"), index=list("abc")
637+
)
638+
df["grouping"] = ["group 1", "group 1", 2]
639+
result = df.groupby("grouping").aggregate(lambda x: x.tolist())
640+
expected_data = [[[0], [0], [0]], [[0, 0], [0, 0], [0, 0]]]
641+
expected = pd.DataFrame(
642+
expected_data,
643+
index=Index([2, "group 1"], dtype="object", name="grouping"),
644+
columns=Index(["X", "Y", "Z"], dtype="object"),
645+
)
646+
tm.assert_frame_equal(result, expected)
647+
648+
633649
class TestLambdaMangling:
634650
def test_basic(self):
635651
df = pd.DataFrame({"A": [0, 0, 1, 1], "B": [1, 2, 3, 4]})

pandas/tests/indexes/interval/test_indexing.py

+12
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ def test_get_indexer_non_unique_with_int_and_float(self, query, expected):
312312
# TODO we may also want to test get_indexer for the case when
313313
# the intervals are duplicated, decreasing, non-monotonic, etc..
314314

315+
def test_get_indexer_non_monotonic(self):
316+
# GH 16410
317+
idx1 = IntervalIndex.from_tuples([(2, 3), (4, 5), (0, 1)])
318+
idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (6, 7), (8, 9)])
319+
result = idx1.get_indexer(idx2)
320+
expected = np.array([2, 0, -1, -1], dtype=np.intp)
321+
tm.assert_numpy_array_equal(result, expected)
322+
323+
result = idx1.get_indexer(idx1[1:])
324+
expected = np.array([1, 2], dtype=np.intp)
325+
tm.assert_numpy_array_equal(result, expected)
326+
315327

316328
class TestSliceLocs:
317329
def test_slice_locs_with_interval(self):

pandas/tests/indexing/multiindex/test_getitem.py

+10
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,13 @@ def test_frame_mi_access_returns_frame(dataframe_with_duplicate_index):
250250
).T
251251
result = df["A"]["B2"]
252252
tm.assert_frame_equal(result, expected)
253+
254+
255+
def test_frame_mi_empty_slice():
256+
# GH 15454
257+
df = DataFrame(0, index=range(2), columns=MultiIndex.from_product([[1], [2]]))
258+
result = df[[]]
259+
expected = DataFrame(
260+
index=[0, 1], columns=MultiIndex(levels=[[1], [2]], codes=[[], []])
261+
)
262+
tm.assert_frame_equal(result, expected)

pandas/tests/indexing/multiindex/test_loc.py

+19
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,22 @@ def test_loc_period_string_indexing():
468468
),
469469
)
470470
tm.assert_series_equal(result, expected)
471+
472+
473+
def test_loc_datetime_mask_slicing():
474+
# GH 16699
475+
dt_idx = pd.to_datetime(["2017-05-04", "2017-05-05"])
476+
m_idx = pd.MultiIndex.from_product([dt_idx, dt_idx], names=["Idx1", "Idx2"])
477+
df = pd.DataFrame(
478+
data=[[1, 2], [3, 4], [5, 6], [7, 6]], index=m_idx, columns=["C1", "C2"]
479+
)
480+
result = df.loc[(dt_idx[0], (df.index.get_level_values(1) > "2017-05-04")), "C1"]
481+
expected = pd.Series(
482+
[3],
483+
name="C1",
484+
index=MultiIndex.from_tuples(
485+
[(pd.Timestamp("2017-05-04"), pd.Timestamp("2017-05-05"))],
486+
names=["Idx1", "Idx2"],
487+
),
488+
)
489+
tm.assert_series_equal(result, expected)

pandas/tests/resample/test_resampler_grouper.py

+17
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ def f(x):
230230
tm.assert_series_equal(result, expected)
231231

232232

233+
def test_apply_columns_multilevel():
234+
# GH 16231
235+
cols = pd.MultiIndex.from_tuples([("A", "a", "", "one"), ("B", "b", "i", "two")])
236+
ind = date_range(start="2017-01-01", freq="15Min", periods=8)
237+
df = DataFrame(np.array([0] * 16).reshape(8, 2), index=ind, columns=cols)
238+
agg_dict = {col: (np.sum if col[3] == "one" else np.mean) for col in df.columns}
239+
result = df.resample("H").apply(lambda x: agg_dict[x.name](x))
240+
expected = DataFrame(
241+
np.array([0] * 4).reshape(2, 2),
242+
index=date_range(start="2017-01-01", freq="1H", periods=2),
243+
columns=pd.MultiIndex.from_tuples(
244+
[("A", "a", "", "one"), ("B", "b", "i", "two")]
245+
),
246+
)
247+
tm.assert_frame_equal(result, expected)
248+
249+
233250
def test_resample_groupby_with_label():
234251
# GH 13235
235252
index = date_range("2000-01-01", freq="2D", periods=5)

pandas/tests/reshape/test_pivot.py

+40
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,46 @@ def test_crosstab_unsorted_order(self):
26492649
)
26502650
tm.assert_frame_equal(result, expected)
26512651

2652+
def test_crosstab_normalize_multiple_columns(self):
2653+
# GH 15150
2654+
df = pd.DataFrame(
2655+
{
2656+
"A": ["one", "one", "two", "three"] * 6,
2657+
"B": ["A", "B", "C"] * 8,
2658+
"C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 4,
2659+
"D": [0] * 24,
2660+
"E": [0] * 24,
2661+
}
2662+
)
2663+
result = pd.crosstab(
2664+
[df.A, df.B],
2665+
df.C,
2666+
values=df.D,
2667+
aggfunc=np.sum,
2668+
normalize=True,
2669+
margins=True,
2670+
)
2671+
expected = pd.DataFrame(
2672+
np.array([0] * 29 + [1], dtype=float).reshape(10, 3),
2673+
columns=Index(["bar", "foo", "All"], dtype="object", name="C"),
2674+
index=MultiIndex.from_tuples(
2675+
[
2676+
("one", "A"),
2677+
("one", "B"),
2678+
("one", "C"),
2679+
("three", "A"),
2680+
("three", "B"),
2681+
("three", "C"),
2682+
("two", "A"),
2683+
("two", "B"),
2684+
("two", "C"),
2685+
("All", ""),
2686+
],
2687+
names=["A", "B"],
2688+
),
2689+
)
2690+
tm.assert_frame_equal(result, expected)
2691+
26522692
def test_margin_normalize(self):
26532693
# GH 27500
26542694
df = pd.DataFrame(

0 commit comments

Comments
 (0)