Skip to content

Commit a76adb3

Browse files
committed
Merge remote-tracking branch 'upstream/master' into docfix-multiindex-set_levels
2 parents 4a8e66a + 641051f commit a76adb3

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

pandas/tests/groupby/test_groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,20 @@ def test_groupby_multiple_columns(df, op):
586586
tm.assert_series_equal(result, expected)
587587

588588

589+
def test_as_index_select_column():
590+
# GH 5764
591+
df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"])
592+
result = df.groupby("A", as_index=False)["B"].get_group(1)
593+
expected = pd.Series([2, 4], name="B")
594+
tm.assert_series_equal(result, expected)
595+
596+
result = df.groupby("A", as_index=False)["B"].apply(lambda x: x.cumsum())
597+
expected = pd.Series(
598+
[2, 6, 6], name="B", index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (1, 2)])
599+
)
600+
tm.assert_series_equal(result, expected)
601+
602+
589603
def test_groupby_as_index_agg(df):
590604
grouped = df.groupby("A", as_index=False)
591605

pandas/tests/groupby/test_transform.py

+67
Original file line numberDiff line numberDiff line change
@@ -1103,3 +1103,70 @@ def test_transform_lambda_with_datetimetz():
11031103
name="time",
11041104
)
11051105
tm.assert_series_equal(result, expected)
1106+
1107+
1108+
def test_transform_fastpath_raises():
1109+
# GH#29631 case where fastpath defined in groupby.generic _choose_path
1110+
# raises, but slow_path does not
1111+
1112+
df = pd.DataFrame({"A": [1, 1, 2, 2], "B": [1, -1, 1, 2]})
1113+
gb = df.groupby("A")
1114+
1115+
def func(grp):
1116+
# we want a function such that func(frame) fails but func.apply(frame)
1117+
# works
1118+
if grp.ndim == 2:
1119+
# Ensure that fast_path fails
1120+
raise NotImplementedError("Don't cross the streams")
1121+
return grp * 2
1122+
1123+
# Check that the fastpath raises, see _transform_general
1124+
obj = gb._obj_with_exclusions
1125+
gen = gb.grouper.get_iterator(obj, axis=gb.axis)
1126+
fast_path, slow_path = gb._define_paths(func)
1127+
_, group = next(gen)
1128+
1129+
with pytest.raises(NotImplementedError, match="Don't cross the streams"):
1130+
fast_path(group)
1131+
1132+
result = gb.transform(func)
1133+
1134+
expected = pd.DataFrame([2, -2, 2, 4], columns=["B"])
1135+
tm.assert_frame_equal(result, expected)
1136+
1137+
1138+
def test_transform_lambda_indexing():
1139+
# GH 7883
1140+
df = pd.DataFrame(
1141+
{
1142+
"A": ["foo", "bar", "foo", "bar", "foo", "flux", "foo", "flux"],
1143+
"B": ["one", "one", "two", "three", "two", "six", "five", "three"],
1144+
"C": range(8),
1145+
"D": range(8),
1146+
"E": range(8),
1147+
}
1148+
)
1149+
df = df.set_index(["A", "B"])
1150+
df = df.sort_index()
1151+
result = df.groupby(level="A").transform(lambda x: x.iloc[-1])
1152+
expected = DataFrame(
1153+
{
1154+
"C": [3, 3, 7, 7, 4, 4, 4, 4],
1155+
"D": [3, 3, 7, 7, 4, 4, 4, 4],
1156+
"E": [3, 3, 7, 7, 4, 4, 4, 4],
1157+
},
1158+
index=MultiIndex.from_tuples(
1159+
[
1160+
("bar", "one"),
1161+
("bar", "three"),
1162+
("flux", "six"),
1163+
("flux", "three"),
1164+
("foo", "five"),
1165+
("foo", "one"),
1166+
("foo", "two"),
1167+
("foo", "two"),
1168+
],
1169+
names=["A", "B"],
1170+
),
1171+
)
1172+
tm.assert_frame_equal(result, expected)

pandas/tests/indexes/test_numeric.py

+6
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ def test_get_indexer(self):
736736
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
737737
tm.assert_numpy_array_equal(indexer, expected)
738738

739+
def test_get_indexer_nan(self):
740+
# GH 7820
741+
result = Index([1, 2, np.nan]).get_indexer([np.nan])
742+
expected = np.array([2], dtype=np.intp)
743+
tm.assert_numpy_array_equal(result, expected)
744+
739745
def test_intersection(self):
740746
index = self.create_index()
741747
other = Index([1, 2, 3, 4, 5])

pandas/tests/indexing/test_loc.py

+14
Original file line numberDiff line numberDiff line change
@@ -966,3 +966,17 @@ def test_loc_getitem_label_list_integer_labels(
966966
expected = df.iloc[:, expected_columns]
967967
result = df.loc[["A", "B", "C"], column_key]
968968
tm.assert_frame_equal(result, expected, check_column_type=check_column_type)
969+
970+
971+
def test_loc_setitem_float_intindex():
972+
# GH 8720
973+
rand_data = np.random.randn(8, 4)
974+
result = pd.DataFrame(rand_data)
975+
result.loc[:, 0.5] = np.nan
976+
expected_data = np.hstack((rand_data, np.array([np.nan] * 8).reshape(8, 1)))
977+
expected = pd.DataFrame(expected_data, columns=[0.0, 1.0, 2.0, 3.0, 0.5])
978+
tm.assert_frame_equal(result, expected)
979+
980+
result = pd.DataFrame(rand_data)
981+
result.loc[:, 0.5] = np.nan
982+
tm.assert_frame_equal(result, expected)

pandas/tests/io/formats/test_to_csv.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import os
23
import sys
34

@@ -571,3 +572,17 @@ def test_to_csv_na_rep_long_string(self, df_new_type):
571572
result = df.to_csv(index=False, na_rep="mynull", encoding="ascii")
572573

573574
assert expected == result
575+
576+
def test_to_csv_timedelta_precision(self):
577+
# GH 6783
578+
s = pd.Series([1, 1]).astype("timedelta64[ns]")
579+
buf = io.StringIO()
580+
s.to_csv(buf)
581+
result = buf.getvalue()
582+
expected_rows = [
583+
",0",
584+
"0,0 days 00:00:00.000000001",
585+
"1,0 days 00:00:00.000000001",
586+
]
587+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
588+
assert result == expected

pandas/tests/io/parser/test_common.py

+10
Original file line numberDiff line numberDiff line change
@@ -2207,3 +2207,13 @@ def test_first_row_bom(all_parsers):
22072207
result = parser.read_csv(StringIO(data), delimiter="\t")
22082208
expected = DataFrame(columns=["Head1", "Head2", "Head3"])
22092209
tm.assert_frame_equal(result, expected)
2210+
2211+
2212+
def test_integer_precision(all_parsers):
2213+
# Gh 7072
2214+
s = """1,1;0;0;0;1;1;3844;3844;3844;1;1;1;1;1;1;0;0;1;1;0;0,,,4321583677327450765
2215+
5,1;0;0;0;1;1;843;843;843;1;1;1;1;1;1;0;0;1;1;0;0,64.0,;,4321113141090630389"""
2216+
parser = all_parsers
2217+
result = parser.read_csv(StringIO(s), header=None)[4]
2218+
expected = Series([4321583677327450765, 4321113141090630389], name=4)
2219+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)