Skip to content

Commit 2019af4

Browse files
committed
Shift test functions up
1 parent 9857dc6 commit 2019af4

File tree

1 file changed

+79
-79
lines changed

1 file changed

+79
-79
lines changed

pandas/tests/groupby/test_transform.py

+79-79
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,84 @@ def test_transform_transformation_func(transformation_func):
356356
tm.assert_frame_equal(result, expected)
357357

358358

359+
def test_groupby_corrwith(transformation_func, df_for_transformation_func):
360+
361+
# GH 27905
362+
df = df_for_transformation_func.copy()
363+
g = df.groupby("A")
364+
365+
if transformation_func == "corrwith":
366+
op = lambda x: getattr(x, transformation_func)(df)
367+
result = op(g)
368+
expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3))
369+
expected.index = pd.Index([121, 231, 676], name="A")
370+
tm.assert_frame_equal(result, expected)
371+
372+
373+
def test_groupby_transform_nan(transformation_func, df_for_transformation_func):
374+
375+
# GH 27905
376+
df = df_for_transformation_func.copy()
377+
g = df.groupby("A")
378+
379+
if transformation_func == "fillna":
380+
381+
df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4]
382+
result = g.transform(transformation_func, value=1)
383+
expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]})
384+
tm.assert_frame_equal(result, expected)
385+
op = lambda x: getattr(x, transformation_func)(1)
386+
result = op(g)
387+
tm.assert_frame_equal(result, expected)
388+
389+
390+
def test_groupby_tshift(transformation_func, df_for_transformation_func):
391+
392+
# GH 27905
393+
df = df_for_transformation_func.copy()
394+
dt_periods = pd.date_range("2013-11-03", periods=7, freq="D")
395+
df["C"] = dt_periods
396+
g = df.set_index("C").groupby("A")
397+
398+
if transformation_func == "tshift":
399+
400+
op = lambda x: getattr(x, transformation_func)(2, "D")
401+
result = op(g)
402+
df["C"] = dt_periods + dt_periods.freq * 2
403+
expected = df
404+
tm.assert_frame_equal(
405+
result.reset_index().reindex(columns=["A", "B", "C"]), expected
406+
)
407+
408+
409+
def test_check_original_and_transformed_index(transformation_func):
410+
411+
# GH 27905
412+
df = DataFrame(
413+
{
414+
"A": [121, 121, 121, 121, 231, 231, 676],
415+
"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0],
416+
}
417+
)
418+
419+
df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]})
420+
g = df.groupby("A")
421+
422+
if transformation_func in [
423+
"cummax",
424+
"cummin",
425+
"cumprod",
426+
"cumsum",
427+
"diff",
428+
"ffill",
429+
"pct_change",
430+
"rank",
431+
"shift",
432+
]:
433+
result = g.transform(transformation_func)
434+
tm.assert_index_equal(result.index, df.index)
435+
436+
359437
def test_transform_select_columns(df):
360438
f = lambda x: x.mean()
361439
result = df.groupby("A")[["C", "D"]].transform(f)
@@ -1205,82 +1283,4 @@ def test_transform_lambda_indexing():
12051283
names=["A", "B"],
12061284
),
12071285
)
1208-
tm.assert_frame_equal(result, expected)
1209-
1210-
1211-
def test_groupby_corrwith(transformation_func, df_for_transformation_func):
1212-
1213-
# GH 27905
1214-
df = df_for_transformation_func.copy()
1215-
g = df.groupby("A")
1216-
1217-
if transformation_func == "corrwith":
1218-
op = lambda x: getattr(x, transformation_func)(df)
1219-
result = op(g)
1220-
expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3))
1221-
expected.index = pd.Index([121, 231, 676], name="A")
1222-
tm.assert_frame_equal(result, expected)
1223-
1224-
1225-
def test_groupby_transform_nan(transformation_func, df_for_transformation_func):
1226-
1227-
# GH 27905
1228-
df = df_for_transformation_func.copy()
1229-
g = df.groupby("A")
1230-
1231-
if transformation_func == "fillna":
1232-
1233-
df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4]
1234-
result = g.transform(transformation_func, value=1)
1235-
expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]})
1236-
tm.assert_frame_equal(result, expected)
1237-
op = lambda x: getattr(x, transformation_func)(1)
1238-
result = op(g)
1239-
tm.assert_frame_equal(result, expected)
1240-
1241-
1242-
def test_groupby_tshift(transformation_func, df_for_transformation_func):
1243-
1244-
# GH 27905
1245-
df = df_for_transformation_func.copy()
1246-
dt_periods = pd.date_range("2013-11-03", periods=7, freq="D")
1247-
df["C"] = dt_periods
1248-
g = df.set_index("C").groupby("A")
1249-
1250-
if transformation_func == "tshift":
1251-
1252-
op = lambda x: getattr(x, transformation_func)(2, "D")
1253-
result = op(g)
1254-
df["C"] = dt_periods + dt_periods.freq * 2
1255-
expected = df
1256-
tm.assert_frame_equal(
1257-
result.reset_index().reindex(columns=["A", "B", "C"]), expected
1258-
)
1259-
1260-
1261-
def test_check_original_and_transformed_index(transformation_func):
1262-
1263-
# GH 27905
1264-
df = DataFrame(
1265-
{
1266-
"A": [121, 121, 121, 121, 231, 231, 676],
1267-
"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0],
1268-
}
1269-
)
1270-
1271-
df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]})
1272-
g = df.groupby("A")
1273-
1274-
if transformation_func in [
1275-
"cummax",
1276-
"cummin",
1277-
"cumprod",
1278-
"cumsum",
1279-
"diff",
1280-
"ffill",
1281-
"pct_change",
1282-
"rank",
1283-
"shift",
1284-
]:
1285-
result = g.transform(transformation_func)
1286-
tm.assert_index_equal(result.index, df.index)
1286+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)