Skip to content

Commit fc36e8b

Browse files
mroeschkeJulianWgs
authored andcommitted
TST: Add tests for old issues 2 (pandas-dev#41493)
1 parent 8b7f9af commit fc36e8b

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

pandas/tests/frame/methods/test_diff.py

+9
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,12 @@ def test_diff_readonly(self):
285285
result = df.diff()
286286
expected = DataFrame(np.array(df)).diff()
287287
tm.assert_frame_equal(result, expected)
288+
289+
def test_diff_all_int_dtype(self, any_int_dtype):
290+
# GH 14773
291+
df = DataFrame(range(5))
292+
df = df.astype(any_int_dtype)
293+
result = df.diff()
294+
expected_dtype = "float32" if any_int_dtype in ("int8", "int16") else "float64"
295+
expected = DataFrame([np.nan, 1.0, 1.0, 1.0, 1.0], dtype=expected_dtype)
296+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_quantile.py

+66
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pandas as pd
55
from pandas import (
66
DataFrame,
7+
Index,
78
Series,
89
Timestamp,
910
)
@@ -650,3 +651,68 @@ def test_quantile_ea_scalar(self, index, frame_or_series):
650651
assert result == expected
651652
else:
652653
tm.assert_series_equal(result, expected)
654+
655+
@pytest.mark.parametrize(
656+
"dtype, expected_data, expected_index, axis",
657+
[
658+
["float64", [], [], 1],
659+
["int64", [], [], 1],
660+
["float64", [np.nan, np.nan], ["a", "b"], 0],
661+
["int64", [np.nan, np.nan], ["a", "b"], 0],
662+
],
663+
)
664+
def test_empty_numeric(self, dtype, expected_data, expected_index, axis):
665+
# GH 14564
666+
df = DataFrame(columns=["a", "b"], dtype=dtype)
667+
result = df.quantile(0.5, axis=axis)
668+
expected = Series(
669+
expected_data, name=0.5, index=Index(expected_index), dtype="float64"
670+
)
671+
tm.assert_series_equal(result, expected)
672+
673+
@pytest.mark.parametrize(
674+
"dtype, expected_data, expected_index, axis, expected_dtype",
675+
[
676+
pytest.param(
677+
"datetime64[ns]",
678+
[],
679+
[],
680+
1,
681+
"datetime64[ns]",
682+
marks=pytest.mark.xfail(reason="#GH 41544"),
683+
),
684+
["datetime64[ns]", [pd.NaT, pd.NaT], ["a", "b"], 0, "datetime64[ns]"],
685+
],
686+
)
687+
def test_empty_datelike(
688+
self, dtype, expected_data, expected_index, axis, expected_dtype
689+
):
690+
# GH 14564
691+
df = DataFrame(columns=["a", "b"], dtype=dtype)
692+
result = df.quantile(0.5, axis=axis, numeric_only=False)
693+
expected = Series(
694+
expected_data, name=0.5, index=Index(expected_index), dtype=expected_dtype
695+
)
696+
tm.assert_series_equal(result, expected)
697+
698+
@pytest.mark.parametrize(
699+
"expected_data, expected_index, axis",
700+
[
701+
[[np.nan, np.nan], range(2), 1],
702+
[[], [], 0],
703+
],
704+
)
705+
def test_datelike_numeric_only(self, expected_data, expected_index, axis):
706+
# GH 14564
707+
df = DataFrame(
708+
{
709+
"a": pd.to_datetime(["2010", "2011"]),
710+
"b": [0, 5],
711+
"c": pd.to_datetime(["2011", "2012"]),
712+
}
713+
)
714+
result = df[["a", "c"]].quantile(0.5, axis=axis)
715+
expected = Series(
716+
expected_data, name=0.5, index=Index(expected_index), dtype=np.float64
717+
)
718+
tm.assert_series_equal(result, expected)

pandas/tests/frame/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -2681,3 +2681,14 @@ def test_from_out_of_bounds_timedelta(self, constructor, cls):
26812681
result = constructor(scalar)
26822682

26832683
assert type(get1(result)) is cls
2684+
2685+
def test_nested_list_columns(self):
2686+
# GH 14467
2687+
result = DataFrame(
2688+
[[1, 2, 3], [4, 5, 6]], columns=[["A", "A", "A"], ["a", "b", "c"]]
2689+
)
2690+
expected = DataFrame(
2691+
[[1, 2, 3], [4, 5, 6]],
2692+
columns=MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")]),
2693+
)
2694+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_stack_unstack.py

+33
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,39 @@ def test_stack_nan_in_multiindex_columns(self):
19991999
)
20002000
tm.assert_frame_equal(result, expected)
20012001

2002+
def test_multi_level_stack_categorical(self):
2003+
# GH 15239
2004+
midx = MultiIndex.from_arrays(
2005+
[
2006+
["A"] * 2 + ["B"] * 2,
2007+
pd.Categorical(list("abab")),
2008+
pd.Categorical(list("ccdd")),
2009+
]
2010+
)
2011+
df = DataFrame(np.arange(8).reshape(2, 4), columns=midx)
2012+
result = df.stack([1, 2])
2013+
expected = DataFrame(
2014+
[
2015+
[0, np.nan],
2016+
[np.nan, 2],
2017+
[1, np.nan],
2018+
[np.nan, 3],
2019+
[4, np.nan],
2020+
[np.nan, 6],
2021+
[5, np.nan],
2022+
[np.nan, 7],
2023+
],
2024+
columns=["A", "B"],
2025+
index=MultiIndex.from_arrays(
2026+
[
2027+
[0] * 4 + [1] * 4,
2028+
pd.Categorical(list("aabbaabb")),
2029+
pd.Categorical(list("cdcdcdcd")),
2030+
]
2031+
),
2032+
)
2033+
tm.assert_frame_equal(result, expected)
2034+
20022035
def test_stack_nan_level(self):
20032036
# GH 9406
20042037
df_nan = DataFrame(

pandas/tests/groupby/test_groupby.py

+17
Original file line numberDiff line numberDiff line change
@@ -2304,3 +2304,20 @@ def test_groupby_mean_duplicate_index(rand_series_with_duplicate_datetimeindex):
23042304
result = dups.groupby(level=0).mean()
23052305
expected = dups.groupby(dups.index).mean()
23062306
tm.assert_series_equal(result, expected)
2307+
2308+
2309+
def test_groupby_all_nan_groups_drop():
2310+
# GH 15036
2311+
s = Series([1, 2, 3], [np.nan, np.nan, np.nan])
2312+
result = s.groupby(s.index).sum()
2313+
expected = Series([], index=Index([], dtype=np.float64), dtype=np.int64)
2314+
tm.assert_series_equal(result, expected)
2315+
2316+
2317+
def test_groupby_empty_multi_column():
2318+
# GH 15106
2319+
result = DataFrame(data=[], columns=["A", "B", "C"]).groupby(["A", "B"]).sum()
2320+
expected = DataFrame(
2321+
[], columns=["C"], index=MultiIndex([[], []], [[], []], names=["A", "B"])
2322+
)
2323+
tm.assert_frame_equal(result, expected)

pandas/tests/io/json/test_pandas.py

+20
Original file line numberDiff line numberDiff line change
@@ -1750,3 +1750,23 @@ def test_readjson_bool_series(self):
17501750
result = read_json("[true, true, false]", typ="series")
17511751
expected = Series([True, True, False])
17521752
tm.assert_series_equal(result, expected)
1753+
1754+
def test_to_json_multiindex_escape(self):
1755+
# GH 15273
1756+
df = DataFrame(
1757+
True,
1758+
index=pd.date_range("2017-01-20", "2017-01-23"),
1759+
columns=["foo", "bar"],
1760+
).stack()
1761+
result = df.to_json()
1762+
expected = (
1763+
"{\"(Timestamp('2017-01-20 00:00:00'), 'foo')\":true,"
1764+
"\"(Timestamp('2017-01-20 00:00:00'), 'bar')\":true,"
1765+
"\"(Timestamp('2017-01-21 00:00:00'), 'foo')\":true,"
1766+
"\"(Timestamp('2017-01-21 00:00:00'), 'bar')\":true,"
1767+
"\"(Timestamp('2017-01-22 00:00:00'), 'foo')\":true,"
1768+
"\"(Timestamp('2017-01-22 00:00:00'), 'bar')\":true,"
1769+
"\"(Timestamp('2017-01-23 00:00:00'), 'foo')\":true,"
1770+
"\"(Timestamp('2017-01-23 00:00:00'), 'bar')\":true}"
1771+
)
1772+
assert result == expected

0 commit comments

Comments
 (0)