-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: DataFrame.append with timedelta64 #39574
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b3fb477
BUG: DataFrame.append with timedelta64
jbrockmendel 8083939
Merge branch 'master' into bug-concat-4
jbrockmendel 512a50c
pretend_axi1 -> ea_compat_axis
jbrockmendel fa6d8a4
Merge branch 'master' into bug-concat-4
jbrockmendel 1c63c05
check compatibility in JoinUnit.is_na
jbrockmendel d75a950
Merge branch 'master' into bug-concat-4
jbrockmendel 5e35e31
Merge branch 'master' into bug-concat-4
jbrockmendel 7de3800
avoid object dtype
jbrockmendel dbee2bc
Merge branch 'master' into bug-concat-4
jbrockmendel dbb59e7
REF: re-use helper func
jbrockmendel f58d791
Merge branch 'master' into bug-concat-4
jbrockmendel f40cf7c
whatsnew
jbrockmendel 7315004
Update pandas/core/internals/concat.py
jbrockmendel faf6c35
Update pandas/core/internals/concat.py
jbrockmendel 3ed534c
revert no-longer necessary
jbrockmendel d1c9872
Merge branch 'bug-concat-4' of github.com:jbrockmendel/pandas into bu…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,29 +334,34 @@ def test_append_missing_column_proper_upcast(self, sort): | |
def test_append_empty_frame_to_series_with_dateutil_tz(self): | ||
# GH 23682 | ||
date = Timestamp("2018-10-24 07:30:00", tz=dateutil.tz.tzutc()) | ||
s = Series({"date": date, "a": 1.0, "b": 2.0}) | ||
ser = Series({"date": date, "a": 1.0, "b": 2.0}) | ||
df = DataFrame(columns=["c", "d"]) | ||
result_a = df.append(s, ignore_index=True) | ||
result_a = df.append(ser, ignore_index=True) | ||
expected = DataFrame( | ||
[[np.nan, np.nan, 1.0, 2.0, date]], columns=["c", "d", "a", "b", "date"] | ||
) | ||
# These columns get cast to object after append | ||
expected["c"] = expected["c"].astype(object) | ||
expected["d"] = expected["d"].astype(object) | ||
expected["date"] = expected["date"].astype(object) | ||
# TODO: "date" might make sense to keep as dt64tz | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tm.assert_frame_equal(result_a, expected) | ||
|
||
expected = DataFrame( | ||
[[np.nan, np.nan, 1.0, 2.0, date]] * 2, columns=["c", "d", "a", "b", "date"] | ||
) | ||
expected["c"] = expected["c"].astype(object) | ||
expected["d"] = expected["d"].astype(object) | ||
|
||
result_b = result_a.append(s, ignore_index=True) | ||
expected["date"] = expected["date"].astype(object) | ||
# TODO: "date" might make sense to keep as dt64tz | ||
result_b = result_a.append(ser, ignore_index=True) | ||
tm.assert_frame_equal(result_b, expected) | ||
|
||
# column order is different | ||
expected = expected[["c", "d", "date", "a", "b"]] | ||
result = df.append([s, s], ignore_index=True) | ||
dtype = Series([date]).dtype | ||
expected["date"] = expected["date"].astype(dtype) | ||
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. Is this still needed? (might be a left-over from astyping it to object before) 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. you're right, updated |
||
result = df.append([ser, ser], ignore_index=True) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_append_empty_tz_frame_with_datetime64ns(self): | ||
|
@@ -378,12 +383,27 @@ def test_append_empty_tz_frame_with_datetime64ns(self): | |
@pytest.mark.parametrize( | ||
"dtype_str", ["datetime64[ns, UTC]", "datetime64[ns]", "Int64", "int64"] | ||
) | ||
def test_append_empty_frame_with_timedelta64ns_nat(self, dtype_str): | ||
@pytest.mark.parametrize("val", [1, "NaT"]) | ||
def test_append_empty_frame_with_timedelta64ns_nat(self, dtype_str, val): | ||
# https://github.com/pandas-dev/pandas/issues/35460 | ||
df = DataFrame(columns=["a"]).astype(dtype_str) | ||
|
||
other = DataFrame({"a": [np.timedelta64("NaT", "ns")]}) | ||
other = DataFrame({"a": [np.timedelta64(val, "ns")]}) | ||
result = df.append(other, ignore_index=True) | ||
|
||
expected = other.astype(object) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"dtype_str", ["datetime64[ns, UTC]", "datetime64[ns]", "Int64", "int64"] | ||
) | ||
@pytest.mark.parametrize("val", [1, "NaT"]) | ||
def test_append_frame_with_timedelta64ns_nat(self, dtype_str, val): | ||
# https://github.com/pandas-dev/pandas/issues/35460 | ||
df = DataFrame({"a": pd.array([1], dtype=dtype_str)}) | ||
|
||
other = DataFrame({"a": [np.timedelta64(val, "ns")]}) | ||
result = df.append(other, ignore_index=True) | ||
|
||
expected = DataFrame({"a": [df.iloc[0, 0], other.iloc[0, 0]]}, dtype=object) | ||
tm.assert_frame_equal(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you clarify what this means in practice to behave "as if axis=1"? Because I assume the arrays are still concatenated with axis=0?
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.
just matters for the dropping-empty check; will edit to clarify