-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
COMPAT: followup to #17491 #17503
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
COMPAT: followup to #17491 #17503
Changes from 1 commit
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 |
---|---|---|
|
@@ -892,18 +892,27 @@ def argmin(self, axis=None): | |
|
||
def tolist(self): | ||
""" | ||
return a list of the values; box to scalars | ||
return a list of the values. These are each a scalar type, which is | ||
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. Capitalize that first sentence (same with |
||
a python scalar (for str, int, float) or a pandas scalar | ||
(for Timestamp/Timedelta/Interval/Period) | ||
|
||
See Also | ||
-------- | ||
numpy.tolist | ||
""" | ||
return list(self.__iter__()) | ||
|
||
if is_datetimelike(self): | ||
return [_maybe_box_datetimelike(x) for x in self._values] | ||
else: | ||
return self._values.tolist() | ||
|
||
def __iter__(self): | ||
""" | ||
provide iteration over the values; box to scalars | ||
return an iterator of the values. These are each a scalar type, | ||
which is a python scalar (for str, int, float) or a pandas scalar | ||
(for Timestamp/Timedelta/Interval/Period) | ||
""" | ||
if is_datetimelike(self): | ||
return (_maybe_box_datetimelike(x) for x in self._values) | ||
else: | ||
return iter(self._values.tolist()) | ||
return iter(self.tolist()) | ||
|
||
@cache_readonly | ||
def hasnans(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,9 +253,8 @@ def get_values(self): | |
""" return the underlying data as an ndarray """ | ||
return self._data.get_values() | ||
|
||
def __iter__(self): | ||
""" iterate like Categorical """ | ||
return self._data.__iter__() | ||
def tolist(self): | ||
return self._data.tolist() | ||
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 don't think this is needed? Base |
||
|
||
@property | ||
def codes(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1054,10 +1054,7 @@ class TestToIterable(object): | |
('timedelta64[ns]', Timedelta)] | ||
|
||
@pytest.mark.parametrize( | ||
'dtype, rdtype', | ||
dtypes + [ | ||
('object', object), | ||
('category', object)]) | ||
'dtype, rdtype', dtypes) | ||
@pytest.mark.parametrize( | ||
'method', | ||
[ | ||
|
@@ -1074,6 +1071,43 @@ def test_iterable(self, typ, method, dtype, rdtype): | |
result = method(s)[0] | ||
assert isinstance(result, rdtype) | ||
|
||
@pytest.mark.parametrize( | ||
'dtype, rdtype, obj', | ||
[ | ||
('object', object, 'a'), | ||
('object', (int, long), 1), | ||
('category', object, 'a'), | ||
('category', (int, long), 1)]) | ||
@pytest.mark.parametrize( | ||
'method', | ||
[ | ||
lambda x: x.tolist(), | ||
lambda x: list(x), | ||
lambda x: list(x.__iter__()), | ||
], ids=['tolist', 'list', 'iter']) | ||
@pytest.mark.parametrize('typ', [Series, Index]) | ||
def test_iterable_object_and_category(self, typ, method, | ||
dtype, rdtype, obj): | ||
# gh-10904 | ||
# gh-13258 | ||
# coerce iteration to underlying python / pandas types | ||
s = typ([obj], dtype=dtype) | ||
result = method(s)[0] | ||
assert isinstance(result, rdtype) | ||
|
||
@pytest.mark.parametrize( | ||
'dtype, rdtype', dtypes) | ||
def test_iterable_items(self, dtype, rdtype): | ||
# gh-13258 | ||
# test items / iteritems yields the correct boxed scalars | ||
# this only applies to series | ||
s = Series([1], dtype=dtype) | ||
_, result = list(s.items())[0] | ||
assert isinstance(result, rdtype) | ||
|
||
_, result = list(s.iteritems())[0] | ||
assert isinstance(result, rdtype) | ||
|
||
@pytest.mark.parametrize( | ||
'dtype, rdtype', | ||
dtypes + [ | ||
|
@@ -1102,3 +1136,40 @@ def test_categorial_datetimelike(self, method): | |
|
||
result = method(i)[0] | ||
assert isinstance(result, Timestamp) | ||
|
||
def test_iter_box(self): | ||
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. Is this just a new test for coverage purposes or related to any of the issues you addressed? 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. a moved test |
||
vals = [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02')] | ||
s = pd.Series(vals) | ||
assert s.dtype == 'datetime64[ns]' | ||
for res, exp in zip(s, vals): | ||
assert isinstance(res, pd.Timestamp) | ||
assert res.tz is None | ||
assert res == exp | ||
|
||
vals = [pd.Timestamp('2011-01-01', tz='US/Eastern'), | ||
pd.Timestamp('2011-01-02', tz='US/Eastern')] | ||
s = pd.Series(vals) | ||
|
||
assert s.dtype == 'datetime64[ns, US/Eastern]' | ||
for res, exp in zip(s, vals): | ||
assert isinstance(res, pd.Timestamp) | ||
assert res.tz == exp.tz | ||
assert res == exp | ||
|
||
# timedelta | ||
vals = [pd.Timedelta('1 days'), pd.Timedelta('2 days')] | ||
s = pd.Series(vals) | ||
assert s.dtype == 'timedelta64[ns]' | ||
for res, exp in zip(s, vals): | ||
assert isinstance(res, pd.Timedelta) | ||
assert res == exp | ||
|
||
# period (object dtype, not boxed) | ||
vals = [pd.Period('2011-01-01', freq='M'), | ||
pd.Period('2011-01-02', freq='M')] | ||
s = pd.Series(vals) | ||
assert s.dtype == 'object' | ||
for res, exp in zip(s, vals): | ||
assert isinstance(res, pd.Period) | ||
assert res.freq == 'M' | ||
assert res == exp |
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.
python --> Python (same thing in the doc-strings that you changed)