Skip to content

Commit

Permalink
Added test case to lock in behaviour (pandas-dev#48541)
Browse files Browse the repository at this point in the history
* Added test case to lock in behaviour

* In previous versions, concatenating to empty EA was resetting type information to np.object

* Update whatsnew

* Addressed code review comments

* whatsnew: made "Loss of dtype" more specific and improved wording (EA -> ExtensionArray, etc)
* testcase: moved test case to test_empty.py and use `tm.assert_frame_equal()` with an expected dataframe

* Fixed style issue

* Use concat/DataFrame as they were directly imported (instead of `pd...`)

* Improved whatsnew entry as per suggestion

Co-authored-by: Sven <you@example.com>
  • Loading branch information
2 people authored and noatamir committed Nov 9, 2022
1 parent d51fae7 commit 174e7ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ Sparse
ExtensionArray
^^^^^^^^^^^^^^
- Bug in :meth:`Series.mean` overflowing unnecessarily with nullable integers (:issue:`48378`)
- Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`)
-

Styler
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/reshape/concat/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,11 @@ def test_concat_empty_dataframe_different_dtypes(self):
result = concat([df1[:0], df2[:0]])
assert result["a"].dtype == np.int64
assert result["b"].dtype == np.object_

def test_concat_to_empty_ea(self):
"""48510 `concat` to an empty EA should maintain type EA dtype."""
df_empty = DataFrame({"a": pd.array([], dtype=pd.Int64Dtype())})
df_new = DataFrame({"a": pd.array([1, 2, 3], dtype=pd.Int64Dtype())})
expected = df_new.copy()
result = concat([df_empty, df_new])
tm.assert_frame_equal(result, expected)

0 comments on commit 174e7ee

Please sign in to comment.