Skip to content

Commit 0ce0bb5

Browse files
authored
TST: Regression testing for fixed issues (#30646)
* Add tests for solved issues * add another test * Clarify test name
1 parent 4df98e6 commit 0ce0bb5

File tree

10 files changed

+175
-1
lines changed

10 files changed

+175
-1
lines changed

pandas/tests/frame/test_constructors.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import OrderedDict, abc
2-
from datetime import datetime, timedelta
2+
from datetime import date, datetime, timedelta
33
import functools
44
import itertools
55

@@ -2425,6 +2425,14 @@ def test_constructor_with_extension_array(self, extension_arr):
24252425
result = DataFrame(extension_arr)
24262426
tm.assert_frame_equal(result, expected)
24272427

2428+
def test_datetime_date_tuple_columns_from_dict(self):
2429+
# GH 10863
2430+
v = date.today()
2431+
tup = v, v
2432+
result = DataFrame({tup: Series(range(3), index=range(3))}, columns=[tup])
2433+
expected = DataFrame([0, 1, 2], columns=pd.Index(pd.Series([tup])))
2434+
tm.assert_frame_equal(result, expected)
2435+
24282436

24292437
class TestDataFrameConstructorWithDatetimeTZ:
24302438
def test_from_dict(self):

pandas/tests/frame/test_missing.py

+13
Original file line numberDiff line numberDiff line change
@@ -970,3 +970,16 @@ def test_interp_ignore_all_good(self):
970970
# all good
971971
result = df[["B", "D"]].interpolate(downcast=None)
972972
tm.assert_frame_equal(result, df[["B", "D"]])
973+
974+
@pytest.mark.parametrize("axis", [0, 1])
975+
def test_interp_time_inplace_axis(self, axis):
976+
# GH 9687
977+
periods = 5
978+
idx = pd.date_range(start="2014-01-01", periods=periods)
979+
data = np.random.rand(periods, periods)
980+
data[data < 0.5] = np.nan
981+
expected = pd.DataFrame(index=idx, columns=idx, data=data)
982+
983+
result = expected.interpolate(axis=0, method="time")
984+
expected.interpolate(axis=0, method="time", inplace=True)
985+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_apply.py

+38
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,41 @@ def test_apply_datetime_issue(group_column_dtlike):
714714
["spam"], Index(["foo"], dtype="object", name="a"), columns=[42]
715715
)
716716
tm.assert_frame_equal(result, expected)
717+
718+
719+
def test_apply_series_return_dataframe_groups():
720+
# GH 10078
721+
tdf = DataFrame(
722+
{
723+
"day": {
724+
0: pd.Timestamp("2015-02-24 00:00:00"),
725+
1: pd.Timestamp("2015-02-24 00:00:00"),
726+
2: pd.Timestamp("2015-02-24 00:00:00"),
727+
3: pd.Timestamp("2015-02-24 00:00:00"),
728+
4: pd.Timestamp("2015-02-24 00:00:00"),
729+
},
730+
"userAgent": {
731+
0: "some UA string",
732+
1: "some UA string",
733+
2: "some UA string",
734+
3: "another UA string",
735+
4: "some UA string",
736+
},
737+
"userId": {
738+
0: "17661101",
739+
1: "17661101",
740+
2: "17661101",
741+
3: "17661101",
742+
4: "17661101",
743+
},
744+
}
745+
)
746+
747+
def most_common_values(df):
748+
return Series({c: s.value_counts().index[0] for c, s in df.iteritems()})
749+
750+
result = tdf.groupby("day").apply(most_common_values)["userId"]
751+
expected = pd.Series(
752+
["17661101"], index=pd.DatetimeIndex(["2015-02-24"], name="day"), name="userId"
753+
)
754+
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_categorical.py

+12
Original file line numberDiff line numberDiff line change
@@ -1330,3 +1330,15 @@ def test_series_groupby_on_2_categoricals_unobserved_zeroes_or_nans(func, zero_o
13301330
# If we expect unobserved values to be zero, we also expect the dtype to be int
13311331
if zero_or_nan == 0:
13321332
assert np.issubdtype(result.dtype, np.integer)
1333+
1334+
1335+
def test_series_groupby_categorical_aggregation_getitem():
1336+
# GH 8870
1337+
d = {"foo": [10, 8, 4, 1], "bar": [10, 20, 30, 40], "baz": ["d", "c", "d", "c"]}
1338+
df = pd.DataFrame(d)
1339+
cat = pd.cut(df["foo"], np.linspace(0, 20, 5))
1340+
df["range"] = cat
1341+
groups = df.groupby(["range", "baz"], as_index=True, sort=True)
1342+
result = groups["foo"].agg("mean")
1343+
expected = groups.agg("mean")["foo"]
1344+
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -2023,3 +2023,10 @@ def test_groupby_crash_on_nunique(axis):
20232023
expected = expected.T
20242024

20252025
tm.assert_frame_equal(result, expected)
2026+
2027+
2028+
def test_groupby_list_level():
2029+
# GH 9790
2030+
expected = pd.DataFrame(np.arange(0, 9).reshape(3, 3))
2031+
result = expected.groupby(level=[0]).mean()
2032+
tm.assert_frame_equal(result, expected)

pandas/tests/indexing/multiindex/test_loc.py

+31
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,34 @@ def test_loc_nan_multiindex():
437437
columns=Index(["d1", "d2", "d3", "d4"], dtype="object"),
438438
)
439439
tm.assert_frame_equal(result, expected)
440+
441+
442+
def test_loc_period_string_indexing():
443+
# GH 9892
444+
a = pd.period_range("2013Q1", "2013Q4", freq="Q")
445+
i = (1111, 2222, 3333)
446+
idx = pd.MultiIndex.from_product((a, i), names=("Periode", "CVR"))
447+
df = pd.DataFrame(
448+
index=idx,
449+
columns=(
450+
"OMS",
451+
"OMK",
452+
"RES",
453+
"DRIFT_IND",
454+
"OEVRIG_IND",
455+
"FIN_IND",
456+
"VARE_UD",
457+
"LOEN_UD",
458+
"FIN_UD",
459+
),
460+
)
461+
result = df.loc[("2013Q1", 1111), "OMS"]
462+
expected = pd.Series(
463+
[np.nan],
464+
dtype=object,
465+
name="OMS",
466+
index=pd.MultiIndex.from_tuples(
467+
[(pd.Period("2013Q1"), 1111)], names=["Periode", "CVR"]
468+
),
469+
)
470+
tm.assert_series_equal(result, expected)

pandas/tests/indexing/test_loc.py

+19
Original file line numberDiff line numberDiff line change
@@ -983,3 +983,22 @@ def test_loc_setitem_float_intindex():
983983
result = pd.DataFrame(rand_data)
984984
result.loc[:, 0.5] = np.nan
985985
tm.assert_frame_equal(result, expected)
986+
987+
988+
def test_loc_axis_1_slice():
989+
# GH 10586
990+
cols = [(yr, m) for yr in [2014, 2015] for m in [7, 8, 9, 10]]
991+
df = pd.DataFrame(
992+
np.ones((10, 8)),
993+
index=tuple("ABCDEFGHIJ"),
994+
columns=pd.MultiIndex.from_tuples(cols),
995+
)
996+
result = df.loc(axis=1)[(2014, 9):(2015, 8)]
997+
expected = pd.DataFrame(
998+
np.ones((10, 4)),
999+
index=tuple("ABCDEFGHIJ"),
1000+
columns=pd.MultiIndex.from_tuples(
1001+
[(2014, 9), (2014, 10), (2015, 7), (2015, 8)]
1002+
),
1003+
)
1004+
tm.assert_frame_equal(result, expected)

pandas/tests/io/parser/test_index_col.py

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from io import StringIO
77

8+
import numpy as np
89
import pytest
910

1011
from pandas import DataFrame, Index, MultiIndex
@@ -172,3 +173,14 @@ def test_multi_index_naming_not_all_at_beginning(all_parsers):
172173
),
173174
)
174175
tm.assert_frame_equal(result, expected)
176+
177+
178+
def test_no_multi_index_level_names_empty(all_parsers):
179+
# GH 10984
180+
parser = all_parsers
181+
midx = MultiIndex.from_tuples([("A", 1, 2), ("A", 1, 2), ("B", 1, 2)])
182+
expected = DataFrame(np.random.randn(3, 3), index=midx, columns=["x", "y", "z"])
183+
with tm.ensure_clean() as path:
184+
expected.to_csv(path)
185+
result = parser.read_csv(path, index_col=[0, 1, 2])
186+
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/test_concat.py

+9
Original file line numberDiff line numberDiff line change
@@ -2730,3 +2730,12 @@ def test_concat_datetimeindex_freq():
27302730
expected = pd.DataFrame(data[50:] + data[:50], index=dr[50:].append(dr[:50]))
27312731
expected.index._data.freq = None
27322732
tm.assert_frame_equal(result, expected)
2733+
2734+
2735+
def test_concat_empty_df_object_dtype():
2736+
# GH 9149
2737+
df_1 = pd.DataFrame({"Row": [0, 1, 1], "EmptyCol": np.nan, "NumberCol": [1, 2, 3]})
2738+
df_2 = pd.DataFrame(columns=df_1.columns)
2739+
result = pd.concat([df_1, df_2], axis=0)
2740+
expected = df_1.astype(object)
2741+
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/test_pivot.py

+25
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,31 @@ def test_pivot_table_aggfunc_scalar_dropna(self, dropna):
19651965

19661966
tm.assert_frame_equal(result, expected)
19671967

1968+
def test_pivot_table_empty_aggfunc(self):
1969+
# GH 9186
1970+
df = pd.DataFrame(
1971+
{
1972+
"A": [2, 2, 3, 3, 2],
1973+
"id": [5, 6, 7, 8, 9],
1974+
"C": ["p", "q", "q", "p", "q"],
1975+
"D": [None, None, None, None, None],
1976+
}
1977+
)
1978+
result = df.pivot_table(index="A", columns="D", values="id", aggfunc=np.size)
1979+
expected = pd.DataFrame()
1980+
tm.assert_frame_equal(result, expected)
1981+
1982+
def test_pivot_table_no_column_raises(self):
1983+
# GH 10326
1984+
def agg(l):
1985+
return np.mean(l)
1986+
1987+
foo = pd.DataFrame(
1988+
{"X": [0, 0, 1, 1], "Y": [0, 1, 0, 1], "Z": [10, 20, 30, 40]}
1989+
)
1990+
with pytest.raises(KeyError, match="notpresent"):
1991+
foo.pivot_table("notpresent", "X", "Y", aggfunc=agg)
1992+
19681993

19691994
class TestCrosstab:
19701995
def setup_method(self, method):

0 commit comments

Comments
 (0)