-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: replace usage internally of .iteritems with .items #26114
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
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 |
---|---|---|
|
@@ -771,15 +771,15 @@ def style(self): | |
|
||
return Styler(self) | ||
|
||
def iteritems(self): | ||
r""" | ||
_shared_docs[ | ||
"items" | ||
] = r""" | ||
Iterator over (column name, Series) pairs. | ||
|
||
Iterates over the DataFrame columns, returning a tuple with | ||
the column name and the content as a Series. | ||
|
||
Yields | ||
------ | ||
%s | ||
label : object | ||
The column names for the DataFrame being iterated over. | ||
content : Series | ||
|
@@ -802,7 +802,7 @@ def iteritems(self): | |
panda bear 1864 | ||
polar bear 22000 | ||
koala marsupial 80000 | ||
>>> for label, content in df.iteritems(): | ||
>>> for label, content in df.items(): | ||
... print('label:', label) | ||
... print('content:', content, sep='\n') | ||
... | ||
|
@@ -819,13 +819,20 @@ def iteritems(self): | |
koala 80000 | ||
Name: population, dtype: int64 | ||
""" | ||
|
||
@Appender(_shared_docs["items"] % "Yields\n ------") | ||
def items(self): | ||
if self.columns.is_unique and hasattr(self, "_item_cache"): | ||
for k in self.columns: | ||
yield k, self._get_item_cache(k) | ||
else: | ||
for i, k in enumerate(self.columns): | ||
yield k, self._ixs(i, axis=1) | ||
|
||
@Appender(_shared_docs["items"] % "Returns\n -------") | ||
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. Both return a generator, so why the different docstring here? 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. It's the "return " part of the method that makes flake8 think this is not a generator, so it won't allow a "Yields" section in the iteritems doc string. I think flake8 is tecnically right, but it makes for uglier code in this case. 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. On second thought, I think maybe if the Appender is followed by a |
||
def iteritems(self): | ||
return self.items() | ||
|
||
def iterrows(self): | ||
""" | ||
Iterate over DataFrame rows as (index, Series) pairs. | ||
|
@@ -843,7 +850,7 @@ def iterrows(self): | |
See Also | ||
-------- | ||
itertuples : Iterate over DataFrame rows as namedtuples of the values. | ||
iteritems : Iterate over (column name, Series) pairs. | ||
items : Iterate over (column name, Series) pairs. | ||
|
||
Notes | ||
----- | ||
|
@@ -901,7 +908,7 @@ def itertuples(self, index=True, name="Pandas"): | |
-------- | ||
DataFrame.iterrows : Iterate over DataFrame rows as (index, Series) | ||
pairs. | ||
DataFrame.iteritems : Iterate over (column name, Series) pairs. | ||
DataFrame.items : Iterate over (column name, Series) pairs. | ||
|
||
Notes | ||
----- | ||
|
@@ -958,8 +965,6 @@ def itertuples(self, index=True, name="Pandas"): | |
# fallback to regular tuples | ||
return zip(*arrays) | ||
|
||
items = iteritems | ||
|
||
def __len__(self): | ||
""" | ||
Returns length of info axis, but here we use the index. | ||
|
@@ -2634,7 +2639,7 @@ def memory_usage(self, index=True, deep=False): | |
5216 | ||
""" | ||
result = Series( | ||
[c.memory_usage(index=False, deep=deep) for col, c in self.iteritems()], | ||
[c.memory_usage(index=False, deep=deep) for col, c in self.items()], | ||
index=self.columns, | ||
) | ||
if index: | ||
|
@@ -4955,7 +4960,7 @@ def f(vals): | |
if not diff.empty: | ||
raise KeyError(diff) | ||
|
||
vals = (col.values for name, col in self.iteritems() if name in subset) | ||
vals = (col.values for name, col in self.items() if name in subset) | ||
labels, shape = map(list, zip(*map(f, vals))) | ||
|
||
ids = get_group_index(labels, shape, sort=False, xnull=False) | ||
|
@@ -7343,7 +7348,7 @@ def round(self, decimals=0, *args, **kwargs): | |
from pandas.core.reshape.concat import concat | ||
|
||
def _dict_round(df, decimals): | ||
for col, vals in df.iteritems(): | ||
for col, vals in df.items(): | ||
try: | ||
yield _series_round(vals, decimals[col]) | ||
except KeyError: | ||
|
@@ -7363,7 +7368,7 @@ def _series_round(s, decimals): | |
new_cols = [col for col in _dict_round(self, decimals)] | ||
elif is_integer(decimals): | ||
# Dispatch to Series.round | ||
new_cols = [_series_round(v, decimals) for _, v in self.iteritems()] | ||
new_cols = [_series_round(v, decimals) for _, v in self.items()] | ||
else: | ||
raise TypeError("decimals must be an integer, a dict-like or a " "Series") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -494,7 +494,7 @@ def _get_space_character_free_column_resolvers(self): | |
""" | ||
from pandas.core.computation.common import _remove_spaces_column_name | ||
|
||
return {_remove_spaces_column_name(k): v for k, v in self.iteritems()} | ||
return {_remove_spaces_column_name(k): v for k, v in self.items()} | ||
|
||
@property | ||
def _info_axis(self): | ||
|
@@ -1936,15 +1936,22 @@ def keys(self): | |
""" | ||
return self._info_axis | ||
|
||
def iteritems(self): | ||
""" | ||
Iterate over (label, values) on info axis | ||
def items(self): | ||
"""Iterate over (label, values) on info axis | ||
|
||
This is index for Series, columns for DataFrame and so on. | ||
This is index for Series and columns for DataFrame. | ||
|
||
Returns | ||
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. Fr the other docstring you used |
||
------- | ||
Generator | ||
""" | ||
for h in self._info_axis: | ||
yield h, self[h] | ||
|
||
@Appender(items.__doc__) | ||
def iteritems(self): | ||
return self.items() | ||
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. Actually, I think both Series and DataFrame have their own implementation, so this one is nowhere used. So can rather be removed? 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'll look into it, |
||
|
||
def __len__(self): | ||
"""Returns length of info axis""" | ||
return len(self._info_axis) | ||
|
@@ -5912,7 +5919,7 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs): | |
"key in a dtype mappings argument." | ||
) | ||
results = [] | ||
for col_name, col in self.iteritems(): | ||
for col_name, col in self.items(): | ||
if col_name in dtype: | ||
results.append( | ||
col.astype( | ||
|
@@ -10328,7 +10335,7 @@ def describe_1d(data): | |
else: | ||
data = self.select_dtypes(include=include, exclude=exclude) | ||
|
||
ldesc = [describe_1d(s) for _, s in data.iteritems()] | ||
ldesc = [describe_1d(s) for _, s in data.items()] | ||
# set a convenient order for rows | ||
names = [] | ||
ldesc_indexes = sorted((x.index for x in ldesc), key=len) | ||
|
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.
did you mean to add this back?
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.
Yes, as we’re not deprecating iteritems anymore?
EDIT: iteritems, not itertools....