Skip to content

PERF: Fixed regression in Series(index=idx) constructor #20865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
maybe_cast_to_datetime, maybe_castable,
construct_1d_arraylike_from_scalar,
construct_1d_object_array_from_listlike)
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
from pandas.core.dtypes.missing import (
isna,
notna,
remove_na_arraylike,
na_value_for_dtype)

from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
Float64Index, _ensure_index)
Expand Down Expand Up @@ -207,10 +211,20 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
else:
data = data.reindex(index, copy=copy)
data = data._data
elif isinstance(data, dict):
elif isinstance(data, dict) and (len(data) or index is None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than doing this here, why not inside _init_dict itself? (you can simply return early if these conditions ar met). you can also return the type, copy parameter as well. ideally like to locate all of a single 'type' code in the same place (dict here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - in particular, _init_dict already checks whether data is empty

# Include the len(data) check here, since _init_dict contains
# a relatively expensive reindex. When called with
# Series(data=None, index=idx`, that is unnescessary. We know
# we're all NaN anyway, so we handle this in the next block.
# https://github.com/pandas-dev/pandas/pull/18496/
data, index = self._init_dict(data, index, dtype)
dtype = None
copy = False
elif isinstance(data, dict):
# Same as previous block, but special cased for data=None,
# for performance when creating empty arrays.
data = na_value_for_dtype(dtype)

elif isinstance(data, SingleBlockManager):
if index is None:
index = data.index
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def test_constructor_nan(self, input_arg):

assert_series_equal(empty, empty2, check_index_type=False)

@pytest.mark.parametrize('dtype', [
'f8', 'i8', 'M8[ns]', 'm8[ns]', 'category', 'object',
'datetime64[ns, UTC]',
])
@pytest.mark.parametrize('index', [None, pd.Index([])])
def test_constructor_dtype_only(self, dtype, index):
# GH-20865
result = pd.Series(dtype=dtype, index=index)
assert result.dtype == dtype
assert len(result) == 0

def test_constructor_series(self):
index1 = ['d', 'b', 'a', 'c']
index2 = sorted(index1)
Expand Down