Skip to content

Commit ad7082b

Browse files
committed
DEPR: Warn about changing empty series dtype
Closes pandas-dev#17261
1 parent 31e77b0 commit ad7082b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ Other API Changes
861861
Deprecations
862862
~~~~~~~~~~~~
863863

864+
- The inferred dtype for an empty Series will change from ``float`` to ``object`` (:issue:`17261`).
864865
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
865866
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
866867
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use ``.astype(object)`` instead (:issue:`18572`)

pandas/core/series.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -4084,20 +4084,30 @@ def _try_cast(arr, take_fast_path):
40844084
subarr = data.copy()
40854085
return subarr
40864086

4087-
elif isinstance(data, (list, tuple)) and len(data) > 0:
4088-
if dtype is not None:
4089-
try:
4090-
subarr = _try_cast(data, False)
4091-
except Exception:
4092-
if raise_cast_failure: # pragma: no cover
4093-
raise
4094-
subarr = np.array(data, dtype=object, copy=copy)
4095-
subarr = lib.maybe_convert_objects(subarr)
4087+
elif isinstance(data, (list, tuple)):
4088+
if len(data) > 0:
4089+
if dtype is not None:
4090+
try:
4091+
subarr = _try_cast(data, False)
4092+
except Exception:
4093+
if raise_cast_failure: # pragma: no cover
4094+
raise
4095+
subarr = np.array(data, dtype=object, copy=copy)
4096+
subarr = lib.maybe_convert_objects(subarr)
40964097

4098+
else:
4099+
subarr = maybe_convert_platform(data)
4100+
subarr = maybe_cast_to_datetime(subarr, dtype)
40974101
else:
4098-
subarr = maybe_convert_platform(data)
4099-
4100-
subarr = maybe_cast_to_datetime(subarr, dtype)
4102+
# subarr = np.array([], dtype=dtype or 'object')
4103+
if dtype is None:
4104+
msg = ("Inferring 'float' dtype for a length-zero array.\n"
4105+
"In a future version of pandas this will change to "
4106+
"'object' dtype.\n\tTo maintain the previous behavior, "
4107+
"use 'dtype=float'.\n\tTo adopt the new behavior, use "
4108+
"'dtype=object'.")
4109+
warnings.warn(msg, FutureWarning, stacklevel=3)
4110+
subarr = _try_cast(data, False)
41014111

41024112
elif isinstance(data, range):
41034113
# GH 16804

0 commit comments

Comments
 (0)