Skip to content

Commit b2b0c9a

Browse files
adminadmin
admin
authored and
admin
committed
took care of review comments
1 parent d303519 commit b2b0c9a

16 files changed

+77
-52
lines changed

doc/source/whatsnew/v1.0.0.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,10 @@ When :class:`Categorical` contains ``np.nan``,
357357
pd.Categorical([1, 2, np.nan], ordered=True).min()
358358
359359
360-
Default dtype of empty :class:`pandas.core.series.Series`
360+
Default dtype of empty :class:`pandas.Series`
361361
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
362362

363-
Initialising an empty :class:`pandas.core.series.Series` without specifying a dtype will raise a `FutureWarning` now
363+
Initialising an empty :class:`pandas.Series` without specifying a dtype will raise a `DeprecationWarning` now
364364
(:issue:`17261`). The default dtype will change from ``float64`` to ``object`` in future releases so that it is
365365
consistent with the behaviour of :class:`DataFrame` and :class:`Index`.
366366

@@ -370,7 +370,7 @@ consistent with the behaviour of :class:`DataFrame` and :class:`Index`.
370370
371371
In [1]: pd.Series()
372372
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.
373+
DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in the next version. Specify a dtype explicitly to silence this warning.
374374
Series([], dtype: float64)
375375
376376
.. _whatsnew_1000.api_breaking.deps:

pandas/core/algorithms.py

+1-1
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/construction.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,21 @@ def _try_cast(
570570
# see gh-17261
571571
def is_empty_data(data):
572572
"""
573-
Utility to check if a Series is instantiated with empty data
573+
Utility to check if a Series is instantiated with empty data,
574+
which does not contain dtype information.
575+
576+
Parameters
577+
----------
578+
data : array-like, Iterable, dict, or scalar value
579+
Contains data stored in Series.
580+
581+
Returns
582+
-------
583+
bool
574584
"""
575585
is_none = data is None
576-
is_simple_empty = isinstance(data, (list, tuple, dict)) and not data
586+
is_list_like_without_dtype = is_list_like(data) and not hasattr(data, "dtype")
587+
is_simple_empty = is_list_like_without_dtype and not data
577588
return is_none or is_simple_empty
578589

579590

@@ -589,14 +600,27 @@ def create_series_with_explicit_dtype(
589600
"""
590601
Helper to pass an explicit dtype when instantiating an empty Series.
591602
592-
The signature of this function mirrors the signature of Series.__init__
593-
but adds the additional keyword argument `dtype_if_empty`.
594-
595-
This silences a FutureWarning described in the GitHub issue
603+
This silences a DeprecationWarning described in the GitHub issue
596604
mentioned above.
605+
606+
Parameters
607+
----------
608+
data : Mirrored from Series.__init__
609+
index : Mirrored from Series.__init__
610+
dtype : Mirrored from Series.__init__
611+
name : Mirrored from Series.__init__
612+
copy : Mirrored from Series.__init__
613+
fastpath : Mirrored from Series.__init__
614+
dtype_if_empty : str, numpy.dtype, or ExtensionDtype
615+
This dtype will be passed explicitly if an empty Series will
616+
be instantiated.
617+
618+
Returns
619+
-------
620+
Series
597621
"""
598622
from pandas.core.series import Series
599623

600624
if is_empty_data(data) and dtype is None:
601625
dtype = dtype_if_empty
602-
return Series(data, index, dtype, name, copy, fastpath)
626+
return Series(data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6070,7 +6070,7 @@ def fillna(
60706070

60716071
if self.ndim == 1:
60726072
if isinstance(value, (dict, ABCSeries)):
6073-
value = create_series_with_explicit_dtype(value)
6073+
value = create_series_with_explicit_dtype(value, dtype_if_empty=object)
60746074
elif not is_list_like(value):
60756075
pass
60766076
else:

pandas/core/groupby/generic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import pandas.core.algorithms as algorithms
5252
from pandas.core.base import DataError, SpecificationError
5353
import pandas.core.common as com
54-
from pandas.core.construction import create_series_with_explicit_dtype
54+
from pandas.core.construction import create_series_with_explicit_dtype, is_empty_data
5555
from pandas.core.frame import DataFrame
5656
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame, _shared_docs
5757
from pandas.core.groupby import base
@@ -260,7 +260,7 @@ def aggregate(self, func=None, *args, **kwargs):
260260
result = self._aggregate_named(func, *args, **kwargs)
261261

262262
index = Index(sorted(result), name=self.grouper.names[0])
263-
ret = create_series_with_explicit_dtype(result, index=index)
263+
ret = create_series_with_explicit_dtype(result, index=index, dtype_if_empty=object)
264264

265265
if not self.as_index: # pragma: no cover
266266
print("Warning, ignoring as_index=True")
@@ -1207,16 +1207,16 @@ def first_not_none(values):
12071207
return DataFrame()
12081208
elif isinstance(v, NDFrame):
12091209

1210-
# this is to silence a FutureWarning
1210+
# this is to silence a DeprecationWarning
12111211
# TODO: Remove when default dtype of empty Series is object
12121212
kwargs = v._construct_axes_dict()
12131213
if v._constructor is Series:
1214-
is_empty = "data" not in kwargs or not kwargs["data"]
1215-
if "dtype" not in kwargs and is_empty:
1216-
kwargs["dtype"] = object
1214+
backup = create_series_with_explicit_dtype(**kwargs, dtype_if_empty=object)
1215+
else:
1216+
backup = v._constructor(**kwargs)
12171217

12181218
values = [
1219-
x if (x is not None) else v._constructor(**kwargs) for x in values
1219+
x if (x is not None) else backup for x in values
12201220
]
12211221

12221222
v = values[0]

pandas/core/series.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,6 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
180180
def __init__(
181181
self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False
182182
):
183-
if is_empty_data(data) and dtype is None:
184-
# Empty Series should have dtype object to be consistent
185-
# with the behaviour of DataFrame and Index
186-
warnings.warn(
187-
"The default dtype for empty Series will be 'object' instead"
188-
" of 'float64' in the next version. Specify a dtype explicitly"
189-
" to silence this warning.",
190-
FutureWarning,
191-
stacklevel=2,
192-
)
193-
# uncomment the line below when removing the FutureWarning
194-
# dtype = np.dtype(object)
195-
196183
# we are called internally, so short-circuit
197184
if fastpath:
198185

@@ -206,6 +193,18 @@ def __init__(
206193

207194
else:
208195

196+
if is_empty_data(data) and dtype is None:
197+
# gh-17261
198+
warnings.warn(
199+
"The default dtype for empty Series will be 'object' instead"
200+
" of 'float64' in the next version. Specify a dtype explicitly"
201+
" to silence this warning.",
202+
DeprecationWarning,
203+
stacklevel=2,
204+
)
205+
# uncomment the line below when removing the DeprecationWarning
206+
# dtype = np.dtype(object)
207+
209208
if index is not None:
210209
index = ensure_index(index)
211210

@@ -345,6 +344,8 @@ def _init_dict(self, data, index=None, dtype=None):
345344
keys, values = [], []
346345

347346
# Input is now list-like, so rely on "standard" construction:
347+
348+
# TODO: passing np.float64 to not break anything yet. See GH-17261
348349
s = create_series_with_explicit_dtype(
349350
values, index=keys, dtype=dtype, dtype_if_empty=np.float64
350351
)

pandas/io/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def _parse_tfoot_tr(self, table):
763763

764764
def _expand_elements(body):
765765
data = [len(elem) for elem in body]
766-
lens = create_series_with_explicit_dtype(data)
766+
lens = create_series_with_explicit_dtype(data, dtype_if_empty=object)
767767
lens_max = lens.max()
768768
not_max = lens[lens != lens_max]
769769

pandas/io/json/_json.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ def _parse_no_numpy(self):
10151015
self.check_keys_split(decoded)
10161016
self.obj = create_series_with_explicit_dtype(**decoded)
10171017
else:
1018-
self.obj = create_series_with_explicit_dtype(data)
1018+
self.obj = create_series_with_explicit_dtype(data, dtype_if_empty=object)
10191019

10201020
def _parse_numpy(self):
10211021
load_kwargs = {
@@ -1033,9 +1033,9 @@ def _parse_numpy(self):
10331033
self.check_keys_split(decoded)
10341034
self.obj = create_series_with_explicit_dtype(**decoded)
10351035
elif self.orient in ["columns", "index"]:
1036-
self.obj = create_series_with_explicit_dtype(*data)
1036+
self.obj = create_series_with_explicit_dtype(*data, dtype_if_empty=object)
10371037
else:
1038-
self.obj = create_series_with_explicit_dtype(data)
1038+
self.obj = create_series_with_explicit_dtype(data, dtype_if_empty=object)
10391039

10401040
def _try_convert_types(self):
10411041
if self.obj is None:

pandas/tests/frame/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ def test_constructor_list_of_series(self):
12171217
OrderedDict([["a", 1.5], ["b", 3], ["c", 4]]),
12181218
OrderedDict([["b", 3], ["c", 4], ["d", 6]]),
12191219
]
1220-
data = [create_series_with_explicit_dtype(d) for d in data]
1220+
data = [create_series_with_explicit_dtype(d, dtype_if_empty=object) for d in data]
12211221

12221222
result = DataFrame(data)
12231223
sdict = OrderedDict(zip(range(len(data)), data))

pandas/tests/io/formats/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ def test_east_asian_unicode_true(self):
10171017
def test_to_string_buffer_all_unicode(self):
10181018
buf = StringIO()
10191019

1020-
empty = DataFrame({"c/\u03c3": Series()})
1020+
empty = DataFrame({"c/\u03c3": Series(dtype=object)})
10211021
nonempty = DataFrame({"c/\u03c3": Series([1, 2, 3])})
10221022

10231023
print(empty, file=buf)

pandas/tests/plotting/test_misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_get_accessor_args():
3434

3535
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
3636
x, y, kind, kwargs = func(
37-
backend_name="", data=Series(), args=["line", None], kwargs={}
37+
backend_name="", data=Series(dtype=object), args=["line", None], kwargs={}
3838
)
3939
assert x is None
4040
assert y is None

pandas/tests/series/test_constructors.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class TestSeriesConstructors:
5252
],
5353
)
5454
def test_empty_constructor(self, constructor, check_index_type):
55-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
55+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
5656
expected = Series()
5757
result = constructor()
5858

@@ -78,7 +78,7 @@ def test_scalar_conversion(self):
7878
assert int(Series([1.0])) == 1
7979

8080
def test_constructor(self, datetime_series):
81-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
81+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
8282
empty_series = Series()
8383
assert datetime_series.index.is_all_dates
8484

@@ -96,7 +96,7 @@ def test_constructor(self, datetime_series):
9696
assert mixed[1] is np.NaN
9797

9898
assert not empty_series.index.is_all_dates
99-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
99+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
100100
assert not Series().index.is_all_dates
101101

102102
# exception raised is of type Exception
@@ -116,7 +116,7 @@ def test_constructor(self, datetime_series):
116116

117117
@pytest.mark.parametrize("input_class", [list, dict, OrderedDict])
118118
def test_constructor_empty(self, input_class):
119-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
119+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
120120
empty = Series()
121121
empty2 = Series(input_class())
122122

@@ -136,7 +136,7 @@ def test_constructor_empty(self, input_class):
136136

137137
if input_class is not list:
138138
# With index:
139-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
139+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
140140
empty = Series(index=range(10))
141141
empty2 = Series(input_class(), index=range(10))
142142
tm.assert_series_equal(empty, empty2)
@@ -170,7 +170,7 @@ def test_constructor_dtype_only(self, dtype, index):
170170
assert len(result) == 0
171171

172172
def test_constructor_no_data_index_order(self):
173-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
173+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
174174
result = pd.Series(index=["b", "a", "c"])
175175
assert result.index.tolist() == ["b", "a", "c"]
176176

@@ -637,7 +637,7 @@ def test_constructor_limit_copies(self, index):
637637
assert s._data.blocks[0].values is not index
638638

639639
def test_constructor_pass_none(self):
640-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
640+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
641641
s = Series(None, index=range(5))
642642
assert s.dtype == np.float64
643643

@@ -646,7 +646,7 @@ def test_constructor_pass_none(self):
646646

647647
# GH 7431
648648
# inference on the index
649-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
649+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
650650
s = Series(index=np.array([None]))
651651
expected = Series(index=Index([None]))
652652
tm.assert_series_equal(s, expected)

pandas/tests/series/test_dtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def test_astype_empty_constructor_equality(self, dtype):
412412
"m", # Generic timestamps raise a ValueError. Already tested.
413413
):
414414
init_empty = Series([], dtype=dtype)
415-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
415+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
416416
as_type_empty = Series([]).astype(dtype)
417417
tm.assert_series_equal(init_empty, as_type_empty)
418418

@@ -497,7 +497,7 @@ def test_infer_objects_series(self):
497497
tm.assert_series_equal(actual, expected)
498498

499499
def test_is_homogeneous_type(self):
500-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
500+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
501501
empty = Series()
502502
assert empty._is_homogeneous_type
503503
assert Series([1, 2])._is_homogeneous_type

pandas/tests/series/test_duplicates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_unique_data_ownership():
7171
)
7272
def test_is_unique(data, expected):
7373
# GH11946 / GH25180
74-
s = create_series_with_explicit_dtype(data)
74+
s = create_series_with_explicit_dtype(data, dtype_if_empty=object)
7575
assert s.is_unique is expected
7676

7777

pandas/tests/series/test_replace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_replace_with_empty_dictlike(self):
246246
s = pd.Series(list("abcd"))
247247
tm.assert_series_equal(s, s.replace(dict()))
248248

249-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
249+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
250250
empty_series = pd.Series([])
251251
tm.assert_series_equal(s, s.replace(empty_series))
252252

pandas/tests/series/test_subclass.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ def test_subclass_unstack(self):
3232
tm.assert_frame_equal(res, exp)
3333

3434
def test_subclass_empty_repr(self):
35-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
35+
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
3636
sub_series = tm.SubclassedSeries()
3737
assert "SubclassedSeries" in repr(sub_series)

0 commit comments

Comments
 (0)