diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index e19aedac802137..4d5ce184554cdf 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -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`) diff --git a/pandas/core/series.py b/pandas/core/series.py index 13e94f971d0032..2e4f554ed8777e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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