-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: need better inference for path in Series construction (GH9456) #9924
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
Changes from all commits
f883b79
a934792
04eeabe
c59c4fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame): | |
Parameters | ||
---------- | ||
data : array-like, dict, or scalar value | ||
Contains data stored in Series | ||
Contains data stored in Series. If a dict and no index is provided, | ||
an attempt will be made to sort the dict. | ||
index : array-like or Index (1d) | ||
Values must be unique and hashable, same length as data. Index | ||
object (or other iterable of same length as data) Will default to | ||
|
@@ -131,6 +132,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None, | |
|
||
else: | ||
|
||
original_index = index | ||
if index is not None: | ||
index = _ensure_index(index) | ||
|
||
|
@@ -162,21 +164,27 @@ def __init__(self, data=None, index=None, dtype=None, name=None, | |
elif isinstance(data, dict): | ||
if index is None: | ||
if isinstance(data, OrderedDict): | ||
index = Index(data) | ||
original_index = data.keys() | ||
else: | ||
index = Index(_try_sort(data)) | ||
original_index = _try_sort(data) | ||
|
||
index = Index(original_index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to use |
||
|
||
try: | ||
if isinstance(index, DatetimeIndex): | ||
# coerce back to datetime objects for lookup | ||
data = lib.fast_multiget(data, index.astype('O'), | ||
default=np.nan) | ||
elif isinstance(index, PeriodIndex): | ||
data = [data.get(i, nan) for i in index] | ||
# lib.fast_multiget raises TypeError if type(data) != dict | ||
|
||
if lib.infer_dtype(data) == lib.infer_dtype(index.values): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I try to avoid a call to |
||
data = lib.fast_multiget(data, index.values, default=np.nan) | ||
else: | ||
data = lib.fast_multiget(data, index.values, | ||
default=np.nan) | ||
except TypeError: | ||
data = [data.get(i, nan) for i in index] | ||
if isinstance(original_index, PeriodIndex): | ||
data = [data.get(i, np.nan) for i in original_index] | ||
else: | ||
# np.array(['z', ('a', 'b')]) raises ValueError; | ||
# this may happens with MultiIndex. | ||
data = lib.fast_multiget(data, np.array(original_index), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
default=np.nan) | ||
except (TypeError, ValueError) as e: | ||
data = [data.get(i, np.nan) for i in index] | ||
|
||
elif isinstance(data, SingleBlockManager): | ||
if index is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it necessary to use a new variable here. This original code looks fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we cannot always reorder the
dict
using theIndex
(because of incompatible data types), I keep the original index to use it if I cannot use theIndex
.