Skip to content

Commit

Permalink
DEPR: Warn about changing empty series dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Apr 23, 2018
1 parent 31e77b0 commit ad7082b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ Other API Changes
Deprecations
~~~~~~~~~~~~

- The inferred dtype for an empty Series will change from ``float`` to ``object`` (:issue:`17261`).
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use ``.astype(object)`` instead (:issue:`18572`)
Expand Down
34 changes: 22 additions & 12 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4084,20 +4084,30 @@ def _try_cast(arr, take_fast_path):
subarr = data.copy()
return subarr

elif isinstance(data, (list, tuple)) and len(data) > 0:
if dtype is not None:
try:
subarr = _try_cast(data, False)
except Exception:
if raise_cast_failure: # pragma: no cover
raise
subarr = np.array(data, dtype=object, copy=copy)
subarr = lib.maybe_convert_objects(subarr)
elif isinstance(data, (list, tuple)):
if len(data) > 0:
if dtype is not None:
try:
subarr = _try_cast(data, False)
except Exception:
if raise_cast_failure: # pragma: no cover
raise
subarr = np.array(data, dtype=object, copy=copy)
subarr = lib.maybe_convert_objects(subarr)

else:
subarr = maybe_convert_platform(data)
subarr = maybe_cast_to_datetime(subarr, dtype)
else:
subarr = maybe_convert_platform(data)

subarr = maybe_cast_to_datetime(subarr, dtype)
# subarr = np.array([], dtype=dtype or 'object')
if dtype is None:
msg = ("Inferring 'float' dtype for a length-zero array.\n"
"In a future version of pandas this will change to "
"'object' dtype.\n\tTo maintain the previous behavior, "
"use 'dtype=float'.\n\tTo adopt the new behavior, use "
"'dtype=object'.")
warnings.warn(msg, FutureWarning, stacklevel=3)
subarr = _try_cast(data, False)

elif isinstance(data, range):
# GH 16804
Expand Down

0 comments on commit ad7082b

Please sign in to comment.