Skip to content

Commit 472af55

Browse files
topper-123jreback
authored andcommitted
CLN: replace usage internally of .iteritems with .items (#26114)
1 parent 15df8a4 commit 472af55

File tree

31 files changed

+84
-70
lines changed

31 files changed

+84
-70
lines changed

asv_bench/benchmarks/frame_methods.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ def setup(self):
115115
)
116116
self.df4 = DataFrame(np.random.randn(N * 1000, 10))
117117

118-
def time_iteritems(self):
118+
def time_items(self):
119119
# (monitor no-copying behaviour)
120120
if hasattr(self.df, "_item_cache"):
121121
self.df._item_cache.clear()
122-
for name, col in self.df.iteritems():
122+
for name, col in self.df.items():
123123
pass
124124

125-
def time_iteritems_cached(self):
126-
for name, col in self.df.iteritems():
125+
def time_items_cached(self):
126+
for name, col in self.df.items():
127127
pass
128128

129129
def time_iteritems_indexing(self):

doc/source/development/contributing_docstring.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ examples:
522522
* ``loc`` and ``iloc``, as they do the same, but in one case providing indices
523523
and in the other positions
524524
* ``max`` and ``min``, as they do the opposite
525-
* ``iterrows``, ``itertuples`` and ``iteritems``, as it is easy that a user
525+
* ``iterrows``, ``itertuples`` and ``items``, as it is easy that a user
526526
looking for the method to iterate over columns ends up in the method to
527527
iterate over rows, and vice-versa
528528
* ``fillna`` and ``dropna``, as both methods are used to handle missing values

doc/source/getting_started/basics.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ Thus, for example, iterating over a DataFrame gives you the column names:
14751475
print(col)
14761476
14771477
1478-
Pandas objects also have the dict-like :meth:`~DataFrame.iteritems` method to
1478+
Pandas objects also have the dict-like :meth:`~DataFrame.items` method to
14791479
iterate over the (key, value) pairs.
14801480

14811481
To iterate over the rows of a DataFrame, you can use the following methods:
@@ -1524,10 +1524,10 @@ To iterate over the rows of a DataFrame, you can use the following methods:
15241524
15251525
df
15261526
1527-
iteritems
1528-
~~~~~~~~~
1527+
items
1528+
~~~~~
15291529

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

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

15381538
.. ipython:: python
15391539
1540-
for label, ser in df.iteritems():
1540+
for label, ser in df.items():
15411541
print(label)
15421542
print(ser)
15431543

doc/source/reference/frame.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ Indexing, iteration
6767
DataFrame.insert
6868
DataFrame.__iter__
6969
DataFrame.items
70-
DataFrame.keys
7170
DataFrame.iteritems
71+
DataFrame.keys
7272
DataFrame.iterrows
7373
DataFrame.itertuples
7474
DataFrame.lookup

doc/source/reference/series.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ Indexing, iteration
7676
Series.loc
7777
Series.iloc
7878
Series.__iter__
79-
Series.iteritems
8079
Series.items
80+
Series.iteritems
8181
Series.keys
8282
Series.pop
8383
Series.item

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ Other deprecations
889889
- :meth:`DataFrame.get_dtype_counts` is deprecated. (:issue:`18262`)
890890
- :meth:`Categorical.ravel` will return a :class:`Categorical` instead of a ``np.ndarray`` (:issue:`27199`)
891891
892+
892893
.. _whatsnew_0250.prior_deprecations:
893894
894895
Removal of prior version deprecations/changes

pandas/core/frame.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -771,15 +771,15 @@ def style(self):
771771

772772
return Styler(self)
773773

774-
def iteritems(self):
775-
r"""
774+
_shared_docs[
775+
"items"
776+
] = r"""
776777
Iterator over (column name, Series) pairs.
777778
778779
Iterates over the DataFrame columns, returning a tuple with
779780
the column name and the content as a Series.
780781
781-
Yields
782-
------
782+
%s
783783
label : object
784784
The column names for the DataFrame being iterated over.
785785
content : Series
@@ -802,7 +802,7 @@ def iteritems(self):
802802
panda bear 1864
803803
polar bear 22000
804804
koala marsupial 80000
805-
>>> for label, content in df.iteritems():
805+
>>> for label, content in df.items():
806806
... print('label:', label)
807807
... print('content:', content, sep='\n')
808808
...
@@ -819,13 +819,20 @@ def iteritems(self):
819819
koala 80000
820820
Name: population, dtype: int64
821821
"""
822+
823+
@Appender(_shared_docs["items"] % "Yields\n ------")
824+
def items(self):
822825
if self.columns.is_unique and hasattr(self, "_item_cache"):
823826
for k in self.columns:
824827
yield k, self._get_item_cache(k)
825828
else:
826829
for i, k in enumerate(self.columns):
827830
yield k, self._ixs(i, axis=1)
828831

832+
@Appender(_shared_docs["items"] % "Returns\n -------")
833+
def iteritems(self):
834+
return self.items()
835+
829836
def iterrows(self):
830837
"""
831838
Iterate over DataFrame rows as (index, Series) pairs.
@@ -843,7 +850,7 @@ def iterrows(self):
843850
See Also
844851
--------
845852
itertuples : Iterate over DataFrame rows as namedtuples of the values.
846-
iteritems : Iterate over (column name, Series) pairs.
853+
items : Iterate over (column name, Series) pairs.
847854
848855
Notes
849856
-----
@@ -901,7 +908,7 @@ def itertuples(self, index=True, name="Pandas"):
901908
--------
902909
DataFrame.iterrows : Iterate over DataFrame rows as (index, Series)
903910
pairs.
904-
DataFrame.iteritems : Iterate over (column name, Series) pairs.
911+
DataFrame.items : Iterate over (column name, Series) pairs.
905912
906913
Notes
907914
-----
@@ -958,8 +965,6 @@ def itertuples(self, index=True, name="Pandas"):
958965
# fallback to regular tuples
959966
return zip(*arrays)
960967

961-
items = iteritems
962-
963968
def __len__(self):
964969
"""
965970
Returns length of info axis, but here we use the index.
@@ -2634,7 +2639,7 @@ def memory_usage(self, index=True, deep=False):
26342639
5216
26352640
"""
26362641
result = Series(
2637-
[c.memory_usage(index=False, deep=deep) for col, c in self.iteritems()],
2642+
[c.memory_usage(index=False, deep=deep) for col, c in self.items()],
26382643
index=self.columns,
26392644
)
26402645
if index:
@@ -4955,7 +4960,7 @@ def f(vals):
49554960
if not diff.empty:
49564961
raise KeyError(diff)
49574962

4958-
vals = (col.values for name, col in self.iteritems() if name in subset)
4963+
vals = (col.values for name, col in self.items() if name in subset)
49594964
labels, shape = map(list, zip(*map(f, vals)))
49604965

49614966
ids = get_group_index(labels, shape, sort=False, xnull=False)
@@ -7343,7 +7348,7 @@ def round(self, decimals=0, *args, **kwargs):
73437348
from pandas.core.reshape.concat import concat
73447349

73457350
def _dict_round(df, decimals):
7346-
for col, vals in df.iteritems():
7351+
for col, vals in df.items():
73477352
try:
73487353
yield _series_round(vals, decimals[col])
73497354
except KeyError:
@@ -7363,7 +7368,7 @@ def _series_round(s, decimals):
73637368
new_cols = [col for col in _dict_round(self, decimals)]
73647369
elif is_integer(decimals):
73657370
# Dispatch to Series.round
7366-
new_cols = [_series_round(v, decimals) for _, v in self.iteritems()]
7371+
new_cols = [_series_round(v, decimals) for _, v in self.items()]
73677372
else:
73687373
raise TypeError("decimals must be an integer, a dict-like or a " "Series")
73697374

pandas/core/generic.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def _get_space_character_free_column_resolvers(self):
494494
"""
495495
from pandas.core.computation.common import _remove_spaces_column_name
496496

497-
return {_remove_spaces_column_name(k): v for k, v in self.iteritems()}
497+
return {_remove_spaces_column_name(k): v for k, v in self.items()}
498498

499499
@property
500500
def _info_axis(self):
@@ -1936,15 +1936,22 @@ def keys(self):
19361936
"""
19371937
return self._info_axis
19381938

1939-
def iteritems(self):
1940-
"""
1941-
Iterate over (label, values) on info axis
1939+
def items(self):
1940+
"""Iterate over (label, values) on info axis
19421941
1943-
This is index for Series, columns for DataFrame and so on.
1942+
This is index for Series and columns for DataFrame.
1943+
1944+
Returns
1945+
-------
1946+
Generator
19441947
"""
19451948
for h in self._info_axis:
19461949
yield h, self[h]
19471950

1951+
@Appender(items.__doc__)
1952+
def iteritems(self):
1953+
return self.items()
1954+
19481955
def __len__(self):
19491956
"""Returns length of info axis"""
19501957
return len(self._info_axis)
@@ -5912,7 +5919,7 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs):
59125919
"key in a dtype mappings argument."
59135920
)
59145921
results = []
5915-
for col_name, col in self.iteritems():
5922+
for col_name, col in self.items():
59165923
if col_name in dtype:
59175924
results.append(
59185925
col.astype(
@@ -10328,7 +10335,7 @@ def describe_1d(data):
1032810335
else:
1032910336
data = self.select_dtypes(include=include, exclude=exclude)
1033010337

10331-
ldesc = [describe_1d(s) for _, s in data.iteritems()]
10338+
ldesc = [describe_1d(s) for _, s in data.items()]
1033210339
# set a convenient order for rows
1033310340
names = []
1033410341
ldesc_indexes = sorted((x.index for x in ldesc), key=len)

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def from_frame(cls, df, sortorder=None, names=None):
601601
if not isinstance(df, ABCDataFrame):
602602
raise TypeError("Input must be a DataFrame")
603603

604-
column_names, columns = zip(*df.iteritems())
604+
column_names, columns = zip(*df.items())
605605
names = column_names if names is None else names
606606
return cls.from_arrays(columns, sortorder=sortorder, names=names)
607607

pandas/core/reshape/pivot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def _compute_grand_margin(data, values, aggfunc, margins_name="All"):
272272

273273
if values:
274274
grand_margin = {}
275-
for k, v in data[values].iteritems():
275+
for k, v in data[values].items():
276276
try:
277277
if isinstance(aggfunc, str):
278278
grand_margin[k] = getattr(v, aggfunc)()

0 commit comments

Comments
 (0)