Skip to content

Commit a6c1ff1

Browse files
JustinZhengBCWillAyd
authored andcommitted
BUG GH22858 When creating empty dataframe, only cast int to float if index given (pandas-dev#22963)
1 parent 8af8cdb commit a6c1ff1

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Other Enhancements
199199

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

203204

204205
.. _whatsnew_0240.api_breaking.interval_values:

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,9 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
12201220
dtype = dtype.dtype
12211221

12221222
# coerce if we have nan for an integer dtype
1223-
if is_integer_dtype(dtype) and isna(value):
1223+
# GH 22858: only cast to float if an index
1224+
# (passed here as length) is specified
1225+
if length and is_integer_dtype(dtype) and isna(value):
12241226
dtype = np.float64
12251227
subarr = np.empty(length, dtype=dtype)
12261228
subarr.fill(value)

pandas/tests/frame/test_constructors.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -798,25 +798,20 @@ def test_constructor_mrecarray(self):
798798
result = DataFrame(mrecs, index=[1, 2])
799799
assert_fr_equal(result, expected)
800800

801-
def test_constructor_corner(self):
801+
def test_constructor_corner_shape(self):
802802
df = DataFrame(index=[])
803803
assert df.values.shape == (0, 0)
804804

805-
# empty but with specified dtype
806-
df = DataFrame(index=lrange(10), columns=['a', 'b'], dtype=object)
807-
assert df.values.dtype == np.object_
808-
809-
# does not error but ends up float
810-
df = DataFrame(index=lrange(10), columns=['a', 'b'], dtype=int)
811-
assert df.values.dtype == np.dtype('float64')
812-
813-
# #1783 empty dtype object
814-
df = DataFrame({}, columns=['foo', 'bar'])
815-
assert df.values.dtype == np.object_
816-
817-
df = DataFrame({'b': 1}, index=lrange(10), columns=list('abc'),
818-
dtype=int)
819-
assert df.values.dtype == np.dtype('float64')
805+
@pytest.mark.parametrize("data, index, columns, dtype, expected", [
806+
(None, lrange(10), ['a', 'b'], object, np.object_),
807+
(None, None, ['a', 'b'], 'int64', np.dtype('int64')),
808+
(None, lrange(10), ['a', 'b'], int, np.dtype('float64')),
809+
({}, None, ['foo', 'bar'], None, np.object_),
810+
({'b': 1}, lrange(10), list('abc'), int, np.dtype('float64'))
811+
])
812+
def test_constructor_dtype(self, data, index, columns, dtype, expected):
813+
df = DataFrame(data, index, columns, dtype)
814+
assert df.values.dtype == expected
820815

821816
def test_constructor_scalar_inference(self):
822817
data = {'int': 1, 'bool': True,

0 commit comments

Comments
 (0)