Skip to content

BUG: Series.__iter__ not dealing with category type well (GH7839) #7842

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 1 commit into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Categoricals in Series/DataFrame
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:class:`~pandas.Categorical` can now be included in `Series` and `DataFrames` and gained new
methods to manipulate. Thanks to Jan Schultz for much of this API/implementation. (:issue:`3943`, :issue:`5313`, :issue:`5314`, :issue:`7444`).
methods to manipulate. Thanks to Jan Schultz for much of this API/implementation. (:issue:`3943`, :issue:`5313`, :issue:`5314`, :issue:`7444`, :issue:`7839`).

For full docs, see the :ref:`Categorical introduction <categorical>` and the :ref:`API documentation <api.categorical>`.

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,9 @@ def _get_repr(
return result

def __iter__(self):
if np.issubdtype(self.dtype, np.datetime64):
if com.is_categorical_dtype(self.dtype):
return iter(self.values)
elif np.issubdtype(self.dtype, np.datetime64):
return (lib.Timestamp(x) for x in self.values)
else:
return iter(self.values)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,27 @@ def test_nan_handling(self):
np.array(["a","b",np.nan], dtype=np.object_))
self.assert_numpy_array_equal(s3.cat._codes, np.array([0,1,2,0]))

def test_sequence_like(self):

# GH 7839
# make sure can iterate
df = DataFrame({"id":[1,2,3,4,5,6], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
df['grade'] = Categorical(df['raw_grade'])

# basic sequencing testing
result = list(df.grade.cat)
expected = np.array(df.grade.cat).tolist()
tm.assert_almost_equal(result,expected)

# iteration
for t in df.itertuples(index=False):
str(t)

for row, s in df.iterrows():
str(s)

for c, col in df.iteritems():
str(s)

def test_series_delegations(self):

Expand Down