Skip to content
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

FIX: define DataFrame.items for all versions of python #17214

Merged
merged 5 commits into from
Aug 19, 2017
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ Other Enhancements
- :func:`DataFrame.add_prefix` and :func:`DataFrame.add_suffix` now accept strings containing the '%' character. (:issue:`17151`)
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`).
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`).
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`)




.. _whatsnew_0210.api_breaking:

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,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 @@ -1110,8 +1110,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
11 changes: 10 additions & 1 deletion pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,16 @@ def test_nonzero(self):
def test_iteritems(self):
df = self.klass([[1, 2, 3], [4, 5, 6]], columns=['a', 'a', 'b'])
for k, v in compat.iteritems(df):
assert type(v) == self.klass._constructor_sliced
assert isinstance(v, self.klass._constructor_sliced)

def test_items(self):
# issue #17213, #13918
cols = ['a', 'b', 'c']
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=cols)
for c, (k, v) in zip(cols, df.items()):
assert c == k
assert isinstance(v, Series)
assert (df[k] == v).all()

def test_iter(self):
assert tm.equalContents(list(self.frame), self.frame.columns)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ def test_iteritems(self):
# assert is lazy (genrators don't define reverse, lists do)
assert not hasattr(self.series.iteritems(), 'reverse')

def test_items(self):
for idx, val in self.series.items():
assert val == self.series[idx]

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

# assert is lazy (genrators don't define reverse, lists do)
assert not hasattr(self.series.items(), 'reverse')

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