Skip to content

BUG GH22858 When creating empty dataframe, only cast int to float if index given #22963

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

Merged
merged 5 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Other Enhancements

Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- A newly constructed empty :class:`DataFrame` with integer as the ``dtype`` will now only be cast to ``float64`` if ``index`` is specified (:issue:`22858`)


.. _whatsnew_0240.api_breaking.interval_values:
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,9 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
dtype = dtype.dtype

# coerce if we have nan for an integer dtype
if is_integer_dtype(dtype) and isna(value):
# GH 22858: only cast to float if an index
# (passed here as length) is specified
if length and is_integer_dtype(dtype) and isna(value):
dtype = np.float64
subarr = np.empty(length, dtype=dtype)
subarr.fill(value)
Expand Down
27 changes: 11 additions & 16 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,25 +798,20 @@ def test_constructor_mrecarray(self):
result = DataFrame(mrecs, index=[1, 2])
assert_fr_equal(result, expected)

def test_constructor_corner(self):
def test_constructor_corner_shape(self):
df = DataFrame(index=[])
assert df.values.shape == (0, 0)

# empty but with specified dtype
df = DataFrame(index=lrange(10), columns=['a', 'b'], dtype=object)
assert df.values.dtype == np.object_

# does not error but ends up float
df = DataFrame(index=lrange(10), columns=['a', 'b'], dtype=int)
assert df.values.dtype == np.dtype('float64')

# #1783 empty dtype object
df = DataFrame({}, columns=['foo', 'bar'])
assert df.values.dtype == np.object_

df = DataFrame({'b': 1}, index=lrange(10), columns=list('abc'),
dtype=int)
assert df.values.dtype == np.dtype('float64')
@pytest.mark.parametrize("data, index, columns, dtype, expected", [
(None, lrange(10), ['a', 'b'], object, np.object_),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u rename ans -> expected

(None, None, ['a', 'b'], 'int64', np.dtype('int64')),
(None, lrange(10), ['a', 'b'], int, np.dtype('float64')),
({}, None, ['foo', 'bar'], None, np.object_),
({'b': 1}, lrange(10), list('abc'), int, np.dtype('float64'))
])
def test_constructor_dtype(self, data, index, columns, dtype, expected):
df = DataFrame(data, index, columns, dtype)
assert df.values.dtype == expected

def test_constructor_scalar_inference(self):
data = {'int': 1, 'bool': True,
Expand Down