Skip to content

Commit 34e91fc

Browse files
committed
Add tests for mean with strings
1 parent ebd91ab commit 34e91fc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/tests/frame/test_reductions.py

+27
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,33 @@ def test_mean_mixed_string_decimal(self):
528528
expected = Series([2.7, 681.6], index=["A", "C"])
529529
tm.assert_series_equal(result, expected)
530530

531+
def test_mean_string(self):
532+
# https://github.com/pandas-dev/pandas/issues/44008
533+
# https://github.com/pandas-dev/pandas/issues/34671
534+
# https://github.com/pandas-dev/pandas/issues/22642
535+
# https://github.com/pandas-dev/pandas/issues/26927
536+
# https://github.com/pandas-dev/pandas/issues/13916
537+
# https://github.com/pandas-dev/pandas/issues/36703
538+
539+
df = DataFrame(
540+
{
541+
"A": ["1", "2", "3"],
542+
"B": ["a", "b", "c"],
543+
"C": [1, 2, 3],
544+
"D": ["0", "1", "J"],
545+
}
546+
)
547+
with tm.assert_produces_warning(FutureWarning, match="Dropping of nuisance"):
548+
result = df.mean()
549+
expected = Series([2.0], index=["C"])
550+
tm.assert_series_equal(result, expected)
551+
msg = "cannot find the mean of type 'str'"
552+
with pytest.raises(TypeError, match=msg):
553+
df.mean(numeric_only=False)
554+
result = df.sum()
555+
expected = Series(["123", "abc", 6, "01J"], index=["A", "B", "C", "D"])
556+
tm.assert_series_equal(result, expected)
557+
531558
def test_var_std(self, datetime_frame):
532559
result = datetime_frame.std(ddof=4)
533560
expected = datetime_frame.apply(lambda x: x.std(ddof=4))

0 commit comments

Comments
 (0)