Skip to content

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

Merged
merged 4 commits into from
Jul 10, 2019
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
8 changes: 4 additions & 4 deletions asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def setup(self):
)
self.df4 = DataFrame(np.random.randn(N * 1000, 10))

def time_iteritems(self):
def time_items(self):
# (monitor no-copying behaviour)
if hasattr(self.df, "_item_cache"):
self.df._item_cache.clear()
for name, col in self.df.iteritems():
for name, col in self.df.items():
pass

def time_iteritems_cached(self):
for name, col in self.df.iteritems():
def time_items_cached(self):
for name, col in self.df.items():
pass

def time_iteritems_indexing(self):
Expand Down
2 changes: 1 addition & 1 deletion doc/source/development/contributing_docstring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ examples:
* ``loc`` and ``iloc``, as they do the same, but in one case providing indices
and in the other positions
* ``max`` and ``min``, as they do the opposite
* ``iterrows``, ``itertuples`` and ``iteritems``, as it is easy that a user
* ``iterrows``, ``itertuples`` and ``items``, as it is easy that a user
looking for the method to iterate over columns ends up in the method to
iterate over rows, and vice-versa
* ``fillna`` and ``dropna``, as both methods are used to handle missing values
Expand Down
10 changes: 5 additions & 5 deletions doc/source/getting_started/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ Thus, for example, iterating over a DataFrame gives you the column names:
print(col)


Pandas objects also have the dict-like :meth:`~DataFrame.iteritems` method to
Pandas objects also have the dict-like :meth:`~DataFrame.items` method to
iterate over the (key, value) pairs.

To iterate over the rows of a DataFrame, you can use the following methods:
Expand Down Expand Up @@ -1524,10 +1524,10 @@ To iterate over the rows of a DataFrame, you can use the following methods:

df

iteritems
~~~~~~~~~
items
~~~~~

Consistent with the dict-like interface, :meth:`~DataFrame.iteritems` iterates
Consistent with the dict-like interface, :meth:`~DataFrame.items` iterates
through key-value pairs:

* **Series**: (index, scalar value) pairs
Expand All @@ -1537,7 +1537,7 @@ For example:

.. ipython:: python

for label, ser in df.iteritems():
for label, ser in df.items():
print(label)
print(ser)

Expand Down
2 changes: 1 addition & 1 deletion doc/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Indexing, iteration
DataFrame.insert
DataFrame.__iter__
DataFrame.items
DataFrame.keys
DataFrame.iteritems
DataFrame.keys
DataFrame.iterrows
DataFrame.itertuples
DataFrame.lookup
Expand Down
2 changes: 1 addition & 1 deletion doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Indexing, iteration
Series.loc
Series.iloc
Series.__iter__
Series.iteritems
Series.items
Series.iteritems
Copy link
Contributor

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?

Copy link
Contributor Author

@topper-123 topper-123 Jul 10, 2019

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....

Series.keys
Series.pop
Series.item
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ Other deprecations
- :meth:`DataFrame.get_dtype_counts` is deprecated. (:issue:`18262`)
- :meth:`Categorical.ravel` will return a :class:`Categorical` instead of a ``np.ndarray`` (:issue:`27199`)


.. _whatsnew_0250.prior_deprecations:

Removal of prior version deprecations/changes
Expand Down
31 changes: 18 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
...
Expand All @@ -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 -------")
Copy link
Member

Choose a reason for hiding this comment

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

Both return a generator, so why the different docstring here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 # noqa comment, we can make flake8 ignore that section. I'll try it tomorrow.

def iteritems(self):
return self.items()

def iterrows(self):
"""
Iterate over DataFrame rows as (index, Series) pairs.
Expand All @@ -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
-----
Expand Down Expand Up @@ -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
-----
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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")

Expand Down
21 changes: 14 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Fr the other docstring you used Yields ?

-------
Generator
"""
for h in self._info_axis:
yield h, self[h]

@Appender(items.__doc__)
def iteritems(self):
return self.items()
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def from_frame(cls, df, sortorder=None, names=None):
if not isinstance(df, ABCDataFrame):
raise TypeError("Input must be a DataFrame")

column_names, columns = zip(*df.iteritems())
column_names, columns = zip(*df.items())
names = column_names if names is None else names
return cls.from_arrays(columns, sortorder=sortorder, names=names)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _compute_grand_margin(data, values, aggfunc, margins_name="All"):

if values:
grand_margin = {}
for k, v in data[values].iteritems():
for k, v in data[values].items():
try:
if isinstance(aggfunc, str):
grand_margin[k] = getattr(v, aggfunc)()
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def _unstack_extension_series(series, level, fill_value):
out = []
values = extract_array(series, extract_numpy=False)

for col, indices in result.iteritems():
for col, indices in result.items():
out.append(
Series(
values.take(indices.values, allow_fill=True, fill_value=fill_value),
Expand Down Expand Up @@ -544,7 +544,7 @@ def factorize(index):
if is_extension_array_dtype(dtype):
arr = dtype.construct_array_type()
new_values = arr._concat_same_type(
[col._values for _, col in frame.iteritems()]
[col._values for _, col in frame.items()]
)
new_values = _reorder_for_extension_array_stack(new_values, N, K)
else:
Expand Down Expand Up @@ -695,7 +695,7 @@ def _convert_level_number(level_num, columns):
subset = this[this.columns[loc]]

value_slice = dtype.construct_array_type()._concat_same_type(
[x._values for _, x in subset.iteritems()]
[x._values for _, x in subset.items()]
)
N, K = this.shape
idx = np.arange(N * K).reshape(K, N).T.ravel()
Expand Down Expand Up @@ -909,7 +909,7 @@ def check_len(item, name):
# columns to prepend to result.
with_dummies = [data.select_dtypes(exclude=dtypes_to_encode)]

for (col, pre, sep) in zip(data_to_encode.iteritems(), prefix, prefix_sep):
for (col, pre, sep) in zip(data_to_encode.items(), prefix, prefix_sep):
# col is (column_name, column), use just column data here
dummy = _get_dummies_1d(
col[1],
Expand Down
13 changes: 7 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,13 +1692,12 @@ def to_string(

# ----------------------------------------------------------------------

def iteritems(self):
def items(self):
"""
Lazily iterate over (index, value) tuples.

This method returns an iterable tuple (index, value). This is
convenient if you want to create a lazy iterator. Note that the
methods Series.items and Series.iteritems are the same methods.
convenient if you want to create a lazy iterator.

Returns
-------
Expand All @@ -1708,20 +1707,22 @@ def iteritems(self):

See Also
--------
DataFrame.iteritems : Equivalent to Series.iteritems for DataFrame.
DataFrame.items : Equivalent to Series.items for DataFrame.

Examples
--------
>>> s = pd.Series(['A', 'B', 'C'])
>>> for index, value in s.iteritems():
>>> for index, value in s.items():
... print("Index : {}, Value : {}".format(index, value))
Index : 0, Value : A
Index : 1, Value : B
Index : 2, Value : C
"""
return zip(iter(self.index), iter(self))

items = iteritems
@Appender(items.__doc__)
def iteritems(self):
return self.items()

# ----------------------------------------------------------------------
# Misc public methods
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def _reindex_index(
need_mask = mask.any()

new_series = {}
for col, series in self.iteritems():
for col, series in self.items():
if mask.all():
continue

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ def str_extractall(arr, pat, flags=0):
index_list = []
is_mi = arr.index.nlevels > 1

for subject_key, subject in arr.iteritems():
for subject_key, subject in arr.items():
if isinstance(subject, str):

if not is_mi:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def hash_pandas_object(
h = Series(h, index=obj.index, dtype="uint64", copy=False)

elif isinstance(obj, ABCDataFrame):
hashes = (hash_array(series.values) for _, series in obj.iteritems())
hashes = (hash_array(series.values) for _, series in obj.items())
num_items = len(obj.columns)
if index:
index_hash_generator = (
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def _update_ctx(self, attrs):
matter.
"""
for row_label, v in attrs.iterrows():
for col_label, col in v.iteritems():
for col_label, col in v.items():
i = self.index.get_indexer([row_label])[0]
j = self.columns.get_indexer([col_label])[0]
for pair in col.rstrip(";").split(";"):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ def _process_converter(self, f, filt=None):

needs_new_obj = False
new_obj = dict()
for i, (col, c) in enumerate(self.obj.iteritems()):
for i, (col, c) in enumerate(self.obj.items()):
if filt(col, c):
new_data, result = f(col, c)
if result:
Expand Down
Loading