-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: inconsistent behaviour for empty DataFrames #48327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c4e83b2
399e209
0d7223a
8f7bc39
ea79bd9
5c945d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3177,3 +3177,35 @@ def test_frame_allow_non_nano(self, arr): | |
def test_frame_from_dict_allow_non_nano(self, arr): | ||
df = DataFrame({0: arr}) | ||
assert df.dtypes[0] == arr.dtype | ||
|
||
|
||
def test_dtype_warning_on_empty_list_df(): | ||
# pd.Series([]) without a specified dtype warns the user | ||
expected = pd.DataFrame({"a": pd.Series([]), "b": pd.Series([])}) | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
# Lists does not warn the user | ||
result = pd.DataFrame({"a": [], "b": []}) | ||
tm.assert_frame_equal(result, expected) # This is true | ||
|
||
|
||
def test_empty_constructs(): | ||
# There should be a consistency for dtype when it's not supplied by the user | ||
result = pd.DataFrame({"a": [], "b": []}) | ||
expected = pd.DataFrame(columns=["a", "b"]) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error
|
||
|
||
|
||
def test_empty_df_without_column_names(): | ||
# Given | ||
result_with_data = pd.DataFrame([1, 2, 3]) | ||
expected_with_data = pd.DataFrame(pd.Series([1, 2, 3])) | ||
# Then | ||
tm.assert_frame_equal(result_with_data, expected_with_data) # True | ||
|
||
# But when it's empty | ||
result_empty = pd.DataFrame([]) | ||
expected_empty = pd.DataFrame(pd.Series([])) | ||
|
||
tm.assert_frame_equal(result_empty, expected_empty) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error returned
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1331,3 +1331,17 @@ def test_result_name_when_one_group(name): | |
expected = Series([1, 2], name=name) | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_apply_on_empty_groupby_dataframe(): | ||
df = pd.DataFrame([(date.today(), 2, 3)], columns=["date", "a", "b"]) | ||
df["date"] = pd.to_datetime(df["date"]) | ||
df = df[df["b"] == 1] # An empty dataframe | ||
result = df.set_index("date").groupby("a", group_keys=True).apply(lambda x: x) | ||
|
||
df2 = pd.DataFrame([(date.today(), 2, 3)], columns=["date", "a", "b"]) | ||
df2["date"] = pd.to_datetime(df2["date"]) | ||
df3 = df2.set_index("date").groupby("a", group_keys=True).apply(lambda x: x) | ||
expected = df3.iloc[:0] # An empty dataframe | ||
|
||
tm.assert_frame_equal(result, expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,3 +142,12 @@ def test_groupby_sample_with_selections(): | |
result = df.groupby("a")[["b", "c"]].sample(n=None, frac=None) | ||
expected = DataFrame({"b": [1, 2], "c": [1, 2]}, index=result.index) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_groupby_sample_with_empty_inputs(): | ||
df = DataFrame({"a": [], "b": []}) | ||
|
||
gb_df = df.groupby("a").sample() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
result = gb_df.empty | ||
expected = True | ||
assert result == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.