Skip to content

BUG: Bug in DataFrame.where with a symmetric shaped frame and a passed other of a DataFrame #7506

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 1 commit into from
Jun 19, 2014
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: 2 additions & 2 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ Experimental

Bug Fixes
~~~~~~~~~
- Bug in ``DataFrame.where`` with a symmetric shaped frame and a passed other of a DataFrame (:issue:`7506`)





- Bug in ``value_counts`` where ``NaT`` did not qualify as missing (``NaN``) (:issue:`7423`)
- Bug in ``value_counts`` where ``NaT`` did not qualify as missing (``NaN``) (:issue:`7423`)



Expand Down
8 changes: 7 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,9 +921,13 @@ def where(self, other, cond, align=True, raise_on_error=True,
if hasattr(other, 'ndim') and hasattr(values, 'ndim'):
if values.ndim != other.ndim or values.shape == other.shape[::-1]:

# if its symmetric are ok, no reshaping needed (GH 7506)
if (values.shape[0] == np.array(values.shape)).all():
pass

# pseodo broadcast (its a 2d vs 1d say and where needs it in a
# specific direction)
if (other.ndim >= 1 and values.ndim - 1 == other.ndim and
elif (other.ndim >= 1 and values.ndim - 1 == other.ndim and
values.shape[0] != other.shape[0]):
other = _block_shape(other).T
else:
Expand All @@ -941,9 +945,11 @@ def where(self, other, cond, align=True, raise_on_error=True,
# may need to undo transpose of values
if hasattr(values, 'ndim'):
if values.ndim != cond.ndim or values.shape == cond.shape[::-1]:

values = values.T
is_transposed = not is_transposed


# our where function
def func(c, v, o):
if c.ravel().all():
Expand Down
48 changes: 35 additions & 13 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5564,27 +5564,27 @@ def test_to_csv_from_csv(self):
with ensure_clean(pname) as path:

self.frame['A'][:5] = nan

self.frame.to_csv(path)
self.frame.to_csv(path, columns=['A', 'B'])
self.frame.to_csv(path, header=False)
self.frame.to_csv(path, index=False)

# test roundtrip
self.tsframe.to_csv(path)
recons = DataFrame.from_csv(path)

assert_frame_equal(self.tsframe, recons)

self.tsframe.to_csv(path, index_label='index')
recons = DataFrame.from_csv(path, index_col=None)
assert(len(recons.columns) == len(self.tsframe.columns) + 1)

# no index
self.tsframe.to_csv(path, index=False)
recons = DataFrame.from_csv(path, index_col=None)
assert_almost_equal(self.tsframe.values, recons.values)

# corner case
dm = DataFrame({'s1': Series(lrange(3), lrange(3)),
's2': Series(lrange(2), lrange(2))})
Expand All @@ -5600,24 +5600,24 @@ def test_to_csv_from_csv(self):
df.to_csv(path)
result = DataFrame.from_csv(path)
assert_frame_equal(result, df)

midx = MultiIndex.from_tuples([('A', 1, 2), ('A', 1, 2), ('B', 1, 2)])
df = DataFrame(np.random.randn(3, 3), index=midx,
columns=['x', 'y', 'z'])
df.to_csv(path)
result = DataFrame.from_csv(path, index_col=[0, 1, 2],
parse_dates=False)
assert_frame_equal(result, df, check_names=False) # TODO from_csv names index ['Unnamed: 1', 'Unnamed: 2'] should it ?

# column aliases
col_aliases = Index(['AA', 'X', 'Y', 'Z'])
self.frame2.to_csv(path, header=col_aliases)
rs = DataFrame.from_csv(path)
xp = self.frame2.copy()
xp.columns = col_aliases

assert_frame_equal(xp, rs)

self.assertRaises(ValueError, self.frame2.to_csv, path,
header=['AA', 'X'])

Expand Down Expand Up @@ -5881,7 +5881,7 @@ def test_to_csv_from_csv_w_some_infs(self):
with ensure_clean() as path:
self.frame.to_csv(path)
recons = DataFrame.from_csv(path)

assert_frame_equal(self.frame, recons, check_names=False) # TODO to_csv drops column name
assert_frame_equal(np.isinf(self.frame), np.isinf(recons), check_names=False)

Expand Down Expand Up @@ -5940,11 +5940,11 @@ def test_to_csv_multiindex(self):

frame.to_csv(path, header=False)
frame.to_csv(path, columns=['A', 'B'])

# round trip
frame.to_csv(path)
df = DataFrame.from_csv(path, index_col=[0, 1], parse_dates=False)

assert_frame_equal(frame, df, check_names=False) # TODO to_csv drops column name
self.assertEqual(frame.index.names, df.index.names)
self.frame.index = old_index # needed if setUP becomes a classmethod
Expand Down Expand Up @@ -9155,6 +9155,28 @@ def test_where_bug(self):
result.where(result > 2, np.nan, inplace=True)
assert_frame_equal(result, expected)

# transpositional issue
# GH7506
a = DataFrame({ 0 : [1,2], 1 : [3,4], 2 : [5,6]})
b = DataFrame({ 0 : [np.nan,8], 1:[9,np.nan], 2:[np.nan,np.nan]})
do_not_replace = b.isnull() | (a > b)

expected = a.copy()
expected[~do_not_replace] = b

result = a.where(do_not_replace,b)
assert_frame_equal(result,expected)

a = DataFrame({ 0 : [4,6], 1 : [1,0]})
b = DataFrame({ 0 : [np.nan,3],1:[3,np.nan]})
do_not_replace = b.isnull() | (a > b)

expected = a.copy()
expected[~do_not_replace] = b

result = a.where(do_not_replace,b)
assert_frame_equal(result,expected)

def test_where_datetime(self):

# GH 3311
Expand Down