Skip to content

Commit abfc78f

Browse files
mroeschkeTLouf
authored andcommitted
TST: More old issues (pandas-dev#41712)
1 parent 8d3d6cd commit abfc78f

File tree

13 files changed

+170
-0
lines changed

13 files changed

+170
-0
lines changed

pandas/tests/frame/indexing/test_setitem.py

+13
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,19 @@ def test_setitem_dtypes_bytes_type_to_object(self):
638638
expected = Series([np.uint32, object, object, np.uint8], index=list("abcd"))
639639
tm.assert_series_equal(result, expected)
640640

641+
def test_boolean_mask_nullable_int64(self):
642+
# GH 28928
643+
result = DataFrame({"a": [3, 4], "b": [5, 6]}).astype(
644+
{"a": "int64", "b": "Int64"}
645+
)
646+
mask = Series(False, index=result.index)
647+
result.loc[mask, "a"] = result["a"]
648+
result.loc[mask, "b"] = result["b"]
649+
expected = DataFrame({"a": [3, 4], "b": [5, 6]}).astype(
650+
{"a": "int64", "b": "Int64"}
651+
)
652+
tm.assert_frame_equal(result, expected)
653+
641654

642655
class TestSetitemTZAwareValues:
643656
@pytest.fixture

pandas/tests/frame/methods/test_append.py

+19
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,22 @@ def test_append_numpy_bug_1681(self, dtype):
238238

239239
result = df.append(other)
240240
assert (result["B"] == index).all()
241+
242+
@pytest.mark.filterwarnings("ignore:The values in the array:RuntimeWarning")
243+
def test_multiindex_column_append_multiple(self):
244+
# GH 29699
245+
df = DataFrame(
246+
[[1, 11], [2, 12], [3, 13]],
247+
columns=pd.MultiIndex.from_tuples(
248+
[("multi", "col1"), ("multi", "col2")], names=["level1", None]
249+
),
250+
)
251+
df2 = df.copy()
252+
for i in range(1, 10):
253+
df[i, "colA"] = 10
254+
df = df.append(df2, ignore_index=True)
255+
result = df["multi"]
256+
expected = DataFrame(
257+
{"col1": [1, 2, 3] * (i + 1), "col2": [11, 12, 13] * (i + 1)}
258+
)
259+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_drop.py

+6
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,9 @@ def test_drop_inplace_no_leftover_column_reference(self):
502502
tm.assert_index_equal(df.columns, Index([], dtype="object"))
503503
a -= a.mean()
504504
tm.assert_index_equal(df.columns, Index([], dtype="object"))
505+
506+
def test_drop_level_missing_label_multiindex(self):
507+
# GH 18561
508+
df = DataFrame(index=MultiIndex.from_product([range(3), range(3)]))
509+
with pytest.raises(KeyError, match="labels \\[5\\] not found in level"):
510+
df.drop(5, level=0)

pandas/tests/frame/methods/test_reindex.py

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ def test_set_reset_index_intervalindex(self):
6060

6161
df = df.reset_index()
6262

63+
def test_setitem_reset_index_dtypes(self):
64+
# GH 22060
65+
df = DataFrame(columns=["a", "b", "c"]).astype(
66+
{"a": "datetime64[ns]", "b": np.int64, "c": np.float64}
67+
)
68+
df1 = df.set_index(["a"])
69+
df1["d"] = []
70+
result = df1.reset_index()
71+
expected = DataFrame(columns=["a", "b", "c", "d"], index=range(0)).astype(
72+
{"a": "datetime64[ns]", "b": np.int64, "c": np.float64, "d": np.float64}
73+
)
74+
tm.assert_frame_equal(result, expected)
75+
76+
df2 = df.set_index(["a", "b"])
77+
df2["d"] = []
78+
result = df2.reset_index()
79+
tm.assert_frame_equal(result, expected)
80+
6381

6482
class TestDataFrameSelectReindex:
6583
# These are specific reindex-based tests; other indexing tests should go in

pandas/tests/frame/methods/test_sort_index.py

+10
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,16 @@ def test_sort_index_ascending_bad_value_raises(self, ascending):
775775
with pytest.raises(ValueError, match=match):
776776
df.sort_index(axis=0, ascending=ascending, na_position="first")
777777

778+
def test_sort_index_use_inf_as_na(self):
779+
# GH 29687
780+
expected = DataFrame(
781+
{"col1": [1, 2, 3], "col2": [3, 4, 5]},
782+
index=pd.date_range("2020", periods=3),
783+
)
784+
with pd.option_context("mode.use_inf_as_na", True):
785+
result = expected.sort_index()
786+
tm.assert_frame_equal(result, expected)
787+
778788

779789
class TestDataFrameSortIndexKey:
780790
def test_sort_multi_index_key(self):

pandas/tests/frame/test_repr_info.py

+8
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,11 @@ def test_frame_to_string_with_periodindex(self):
322322

323323
# it works!
324324
frame.to_string()
325+
326+
def test_datetime64tz_slice_non_truncate(self):
327+
# GH 30263
328+
df = DataFrame({"x": date_range("2019", periods=10, tz="UTC")})
329+
expected = repr(df)
330+
df = df.iloc[:, :5]
331+
result = repr(df)
332+
assert result == expected

pandas/tests/groupby/test_groupby.py

+21
Original file line numberDiff line numberDiff line change
@@ -2338,3 +2338,24 @@ def test_groupby_filtered_df_std():
23382338
index=Index([True], name="groupby_col"),
23392339
)
23402340
tm.assert_frame_equal(result, expected)
2341+
2342+
2343+
def test_datetime_categorical_multikey_groupby_indices():
2344+
# GH 26859
2345+
df = DataFrame(
2346+
{
2347+
"a": Series(list("abc")),
2348+
"b": Series(
2349+
to_datetime(["2018-01-01", "2018-02-01", "2018-03-01"]),
2350+
dtype="category",
2351+
),
2352+
"c": Categorical.from_codes([-1, 0, 1], categories=[0, 1]),
2353+
}
2354+
)
2355+
result = df.groupby(["a", "b"]).indices
2356+
expected = {
2357+
("a", Timestamp("2018-01-01 00:00:00")): np.array([0]),
2358+
("b", Timestamp("2018-02-01 00:00:00")): np.array([1]),
2359+
("c", Timestamp("2018-03-01 00:00:00")): np.array([2]),
2360+
}
2361+
assert result == expected

pandas/tests/groupby/test_nth.py

+26
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,29 @@ def test_first_categorical_and_datetime_data_nat():
663663
)
664664
expected.index = Index(["first", "second", "third"], name="group")
665665
tm.assert_frame_equal(result, expected)
666+
667+
668+
def test_first_multi_key_groupbby_categorical():
669+
# GH 22512
670+
df = DataFrame(
671+
{
672+
"A": [1, 1, 1, 2, 2],
673+
"B": [100, 100, 200, 100, 100],
674+
"C": ["apple", "orange", "mango", "mango", "orange"],
675+
"D": ["jupiter", "mercury", "mars", "venus", "venus"],
676+
}
677+
)
678+
df = df.astype({"D": "category"})
679+
result = df.groupby(by=["A", "B"]).first()
680+
expected = DataFrame(
681+
{
682+
"C": ["apple", "mango", "mango"],
683+
"D": Series(["jupiter", "mars", "venus"]).astype(
684+
pd.CategoricalDtype(["jupiter", "mars", "mercury", "venus"])
685+
),
686+
}
687+
)
688+
expected.index = MultiIndex.from_tuples(
689+
[(1, 100), (1, 200), (2, 100)], names=["A", "B"]
690+
)
691+
tm.assert_frame_equal(result, expected)

pandas/tests/indexing/test_iloc.py

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pandas.util._test_decorators as td
1414

1515
from pandas import (
16+
NA,
1617
Categorical,
1718
CategoricalDtype,
1819
DataFrame,
@@ -1340,3 +1341,10 @@ def test_iloc_setitem_pure_position_based(self):
13401341
ser1.iloc[1:3] = ser2.iloc[1:3]
13411342
expected = Series([1, 5, 6])
13421343
tm.assert_series_equal(ser1, expected)
1344+
1345+
def test_iloc_nullable_int64_size_1_nan(self):
1346+
# GH 31861
1347+
result = DataFrame({"a": ["test"], "b": [np.nan]})
1348+
result.loc[:, "b"] = result.loc[:, "b"].astype("Int64")
1349+
expected = DataFrame({"a": ["test"], "b": array([NA], dtype="Int64")})
1350+
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/merge/test_merge.py

+8
Original file line numberDiff line numberDiff line change
@@ -2479,3 +2479,11 @@ def test_merge_string_float_column_result():
24792479
[[9, 10, 1, 2], [11, 12, 3, 4]], columns=pd.Index(["x", "y", "a", 114.0])
24802480
)
24812481
tm.assert_frame_equal(result, expected)
2482+
2483+
2484+
def test_mergeerror_on_left_index_mismatched_dtypes():
2485+
# GH 22449
2486+
df_1 = DataFrame(data=["X"], columns=["C"], index=[22])
2487+
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
2488+
with pytest.raises(MergeError, match="Can only pass argument"):
2489+
merge(df_1, df_2, on=["C"], left_index=True)

pandas/tests/series/methods/test_sort_values.py

+7
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ def test_sort_values_pos_args_deprecation(self):
199199
expected = Series([1, 2, 3])
200200
tm.assert_series_equal(result, expected)
201201

202+
def test_mergesort_decending_stability(self):
203+
# GH 28697
204+
s = Series([1, 2, 1, 3], ["first", "b", "second", "c"])
205+
result = s.sort_values(ascending=False, kind="mergesort")
206+
expected = Series([3, 2, 1, 1], ["c", "b", "first", "second"])
207+
tm.assert_series_equal(result, expected)
208+
202209

203210
class TestSeriesSortingKey:
204211
def test_sort_values_key(self):

pandas/tests/tseries/offsets/test_offsets.py

+18
Original file line numberDiff line numberDiff line change
@@ -870,3 +870,21 @@ def test_dateoffset_immutable(attribute):
870870
msg = "DateOffset objects are immutable"
871871
with pytest.raises(AttributeError, match=msg):
872872
setattr(offset, attribute, 5)
873+
874+
875+
@pytest.mark.parametrize(
876+
"weekmask, expected_time, mult",
877+
[
878+
["Mon Tue Wed Thu Fri Sat", "2018-11-10 09:00:00", 10],
879+
["Tue Wed Thu Fri Sat", "2018-11-13 08:00:00", 18],
880+
],
881+
)
882+
def test_custom_businesshour_weekmask_and_holidays(weekmask, expected_time, mult):
883+
# GH 23542
884+
holidays = ["2018-11-09"]
885+
bh = CustomBusinessHour(
886+
start="08:00", end="17:00", weekmask=weekmask, holidays=holidays
887+
)
888+
result = Timestamp("2018-11-08 08:00") + mult * bh
889+
expected = Timestamp(expected_time)
890+
assert result == expected

pandas/tests/window/test_rolling.py

+8
Original file line numberDiff line numberDiff line change
@@ -1411,3 +1411,11 @@ def test_rolling_sum_all_nan_window_floating_artifacts():
14111411
result = df.rolling(3, min_periods=0).sum()
14121412
expected = DataFrame([0.002, 0.010, 0.015, 0.013, 0.005, 0.0])
14131413
tm.assert_frame_equal(result, expected)
1414+
1415+
1416+
def test_rolling_zero_window():
1417+
# GH 22719
1418+
s = Series(range(1))
1419+
result = s.rolling(0).min()
1420+
expected = Series([np.nan])
1421+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)