Skip to content

Commit

Permalink
ENH: added .items to Series and DataFrame for both PY2 and PY3
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisacidic committed Aug 18, 2016
1 parent e5ee5d2 commit d6d93ba
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ API changes
result in ``NaT`` and not ``nan`` (:issue:`11245`).
- Indexing with a null key will raise a ``TypeError``, instead of a ``ValueError`` (:issue:`11356`)
- ``Series.ptp`` will now ignore missing values by default (:issue:`11163`)
- ``Series`` and ``DataFrame`` have gained the .items() method to mirror .iteritems() and for compat with PY3 (:issue:`13918`)

.. _whatsnew_0171.deprecations:

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,7 @@ def itertuples(self, index=True, name="Pandas"):
# fallback to regular tuples
return zip(*arrays)

if compat.PY3: # pragma: no cover
items = iteritems
items = iteritems

def __len__(self):
"""Returns length of info axis, but here we use the index """
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,7 @@ def iteritems(self):
"""
return zip(iter(self.index), iter(self))

if compat.PY3: # pragma: no cover
items = iteritems
items = iteritems

# ----------------------------------------------------------------------
# Misc public methods
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_misc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ def test_iteritems(self):
for k, v in compat.iteritems(df):
self.assertEqual(type(v), Series)

def test_items(self):
# items is the same as iteritems (#13918)
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=['a', 'a', 'b'])
for k, v in df.items():
self.assertEqual(type(v), Series)

def test_iter(self):
self.assertTrue(tm.equalContents(list(self.frame), self.frame.columns))

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_misc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ def test_iteritems(self):
# assert is lazy (genrators don't define reverse, lists do)
self.assertFalse(hasattr(self.series.iteritems(), 'reverse'))

def test_items(self):
# items is the same as iteritems (#13918)
for idx, val in self.series.items():
self.assertEqual(val, self.series[idx])

for idx, val in self.ts.items():
self.assertEqual(val, self.ts[idx])

self.assertFalse(hasattr(self.series.items(), 'reverse'))

def test_raise_on_info(self):
s = Series(np.random.randn(10))
with tm.assertRaises(AttributeError):
Expand Down

0 comments on commit d6d93ba

Please sign in to comment.