Skip to content

Commit d303519

Browse files
SaturnFromTitanadmin
authored andcommitted
added FutureWarning to empty Series without dtype and adjusted the tests and docs so that no unnecessary warnings are thrown
1 parent 83812e1 commit d303519

File tree

83 files changed

+403
-242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+403
-242
lines changed

doc/source/user_guide/missing_data.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ The sum of an empty or all-NA Series or column of a DataFrame is 0.
190190
191191
pd.Series([np.nan]).sum()
192192
193-
pd.Series([]).sum()
193+
pd.Series([], dtype="float64").sum()
194194
195195
The product of an empty or all-NA Series or column of a DataFrame is 1.
196196

197197
.. ipython:: python
198198
199199
pd.Series([np.nan]).prod()
200200
201-
pd.Series([]).prod()
201+
pd.Series([], dtype="float64").prod()
202202
203203
204204
NA values in GroupBy

doc/source/user_guide/scale.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ results will fit in memory, so we can safely call ``compute`` without running
358358
out of memory. At that point it's just a regular pandas object.
359359

360360
.. ipython:: python
361+
:okwarning:
361362
362363
@savefig dask_resample.png
363364
ddf[['x', 'y']].resample("1D").mean().cumsum().compute().plot()

doc/source/whatsnew/v0.19.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ A ``Series`` will now correctly promote its dtype for assignment with incompat v
707707

708708

709709
.. ipython:: python
710+
:okwarning:
710711
711712
s = pd.Series()
712713

doc/source/whatsnew/v0.21.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ Note that this also changes the sum of an empty ``Series``. Previously this alwa
428428
but for consistency with the all-NaN case, this was changed to return NaN as well:
429429

430430
.. ipython:: python
431+
:okwarning:
431432
432433
pd.Series([]).sum()
433434

doc/source/whatsnew/v0.22.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The default sum for empty or all-*NA* ``Series`` is now ``0``.
5555
*pandas 0.22.0*
5656

5757
.. ipython:: python
58+
:okwarning:
5859
5960
pd.Series([]).sum()
6061
pd.Series([np.nan]).sum()
@@ -67,6 +68,7 @@ pandas 0.20.3 without bottleneck, or pandas 0.21.x), use the ``min_count``
6768
keyword.
6869

6970
.. ipython:: python
71+
:okwarning:
7072
7173
pd.Series([]).sum(min_count=1)
7274
@@ -85,6 +87,7 @@ required for a non-NA sum or product.
8587
returning ``1`` instead.
8688

8789
.. ipython:: python
90+
:okwarning:
8891
8992
pd.Series([]).prod()
9093
pd.Series([np.nan]).prod()

doc/source/whatsnew/v1.0.0.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,23 @@ When :class:`Categorical` contains ``np.nan``,
356356
357357
pd.Categorical([1, 2, np.nan], ordered=True).min()
358358
359+
360+
Default dtype of empty :class:`pandas.core.series.Series`
361+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
362+
363+
Initialising an empty :class:`pandas.core.series.Series` without specifying a dtype will raise a `FutureWarning` now
364+
(:issue:`17261`). The default dtype will change from ``float64`` to ``object`` in future releases so that it is
365+
consistent with the behaviour of :class:`DataFrame` and :class:`Index`.
366+
367+
*pandas 1.0.0*
368+
369+
.. code-block:: ipython
370+
371+
In [1]: pd.Series()
372+
Out[2]:
373+
FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in the next version. Specify a dtype explicitly to silence this warning.
374+
Series([], dtype: float64)
375+
359376
.. _whatsnew_1000.api_breaking.deps:
360377

361378
Increased minimum versions for dependencies
@@ -484,7 +501,7 @@ Removal of prior version deprecations/changes
484501

485502
Previously, pandas would register converters with matplotlib as a side effect of importing pandas (:issue:`18720`).
486503
This changed the output of plots made via matplotlib plots after pandas was imported, even if you were using
487-
matplotlib directly rather than rather than :meth:`~DataFrame.plot`.
504+
matplotlib directly rather than :meth:`~DataFrame.plot`.
488505

489506
To use pandas formatters with a matplotlib plot, specify
490507

pandas/compat/pickle_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __new__(cls) -> "Series": # type: ignore
6464
stacklevel=6,
6565
)
6666

67-
return Series()
67+
return Series(dtype=object)
6868

6969

7070
class _LoadSparseFrame:

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def _factorize_array(
601601
)
602602
@Appender(_shared_docs["factorize"])
603603
def factorize(
604-
values, sort: bool = False, na_sentinel: int = -1, size_hint: Optional[int] = None,
604+
values, sort: bool = False, na_sentinel: int = -1, size_hint: Optional[int] = None
605605
) -> Tuple[np.ndarray, Union[np.ndarray, ABCIndex]]:
606606
# Implementation notes: This method is responsible for 3 things
607607
# 1.) coercing data to array-like (ndarray, Index, extension array)

pandas/core/apply.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
)
1616
from pandas.core.dtypes.generic import ABCMultiIndex, ABCSeries
1717

18+
from pandas.core.construction import create_series_with_explicit_dtype
19+
1820
if TYPE_CHECKING:
1921
from pandas import DataFrame, Series, Index
2022

@@ -203,15 +205,15 @@ def apply_empty_result(self):
203205

204206
if not should_reduce:
205207
try:
206-
r = self.f(Series([]))
208+
r = self.f(Series([], dtype=np.float64))
207209
except Exception:
208210
pass
209211
else:
210212
should_reduce = not isinstance(r, Series)
211213

212214
if should_reduce:
213215
if len(self.agg_axis):
214-
r = self.f(Series([]))
216+
r = self.f(Series([], dtype=np.float64))
215217
else:
216218
r = np.nan
217219

@@ -346,14 +348,25 @@ def apply_series_generator(self) -> Tuple[ResType, "Index"]:
346348
def wrap_results(
347349
self, results: ResType, res_index: "Index"
348350
) -> Union["Series", "DataFrame"]:
351+
from pandas import Series
349352

350353
# see if we can infer the results
351354
if len(results) > 0 and 0 in results and is_sequence(results[0]):
352355

353356
return self.wrap_results_for_axis(results, res_index)
354357

355358
# dict of scalars
356-
result = self.obj._constructor_sliced(results)
359+
360+
# the default dtype of an empty Series will be `object`, but this
361+
# code can be hit by df.mean() where the result should have dtype
362+
# float64 even if it's an empty Series.
363+
constructor_sliced = self.obj._constructor_sliced
364+
if constructor_sliced is Series:
365+
result = create_series_with_explicit_dtype(
366+
results, dtype_if_empty=np.float64
367+
)
368+
else:
369+
result = constructor_sliced(results)
357370
result.index = res_index
358371

359372
return result

pandas/core/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from pandas.core.accessor import DirNamesMixin
3535
from pandas.core.algorithms import duplicated, unique1d, value_counts
3636
from pandas.core.arrays import ExtensionArray
37+
from pandas.core.construction import create_series_with_explicit_dtype
3738
import pandas.core.nanops as nanops
3839

3940
_shared_docs: Dict[str, str] = dict()
@@ -1143,9 +1144,14 @@ def _map_values(self, mapper, na_action=None):
11431144
# convert to an Series for efficiency.
11441145
# we specify the keys here to handle the
11451146
# possibility that they are tuples
1146-
from pandas import Series
11471147

1148-
mapper = Series(mapper)
1148+
# The return value of mapping with an empty mapper is
1149+
# expected to be pd.Series(np.nan, ...). As np.nan is
1150+
# of dtype float64 the return value of this method should
1151+
# be float64 as well
1152+
mapper = create_series_with_explicit_dtype(
1153+
mapper, dtype_if_empty=np.float64
1154+
)
11491155

11501156
if isinstance(mapper, ABCSeries):
11511157
# Since values were input this means we came from either

0 commit comments

Comments
 (0)