Skip to content

BUG: Comparisons result in different dtypes for empty DataFrames #15077 #15115

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,4 @@ Bug Fixes

- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)

- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
- Not boolean Series returned by comparison methods (e.g., ``lt``, ``gt``, ...) against a constant for an empty DataFrame (:issue:`15077`)
Copy link
Contributor

Choose a reason for hiding this comment

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

say

Incorrect dtyped Series was returned by comparions methods.........against a constant for an empty DataFrame

put Series/DataFrame with double-backticks.

3 changes: 0 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3595,9 +3595,6 @@ def _combine_match_columns(self, other, func, level=None, fill_value=None):
return self._constructor(new_data)

def _combine_const(self, other, func, raise_on_error=True):
if self.empty:
return self

new_data = self._data.eval(func=func, other=other,
raise_on_error=raise_on_error)
return self._constructor(new_data)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,28 @@ def _test_seq(df, idx_ser, col_ser):
exp = DataFrame({'col': [False, True, False]})
assert_frame_equal(result, exp)

def test_return_dtypes_bool_op_costant(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you replicate these tests for series as well (or point to that we have equiv tests)

# GH15077
df = DataFrame({'x': [1, 2, 3], 'y': [1., 2., 3.]})
const = 2

# not empty DataFrame
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
f = getattr(df, op)
for col in ['x', 'y']:
res = f(const)
c = getattr(res, col)
self.assertEqual(c.dtypes, bool)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here


# empty DataFrame
empty = df.iloc[:0]
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
f = getattr(empty, op)
for col in ['x', 'y']:
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of this comparison, use assert_series_equal and compare against Series([2], ['bool']
where you do result = getattr(empty, op)(const).get_dtype_counts()
e.g

In [5]: df = DataFrame({'x': [1, 2, 3], 'y': [1., 2., 3.]})

In [6]: df.lt(2)
Out[6]: 
       x      y
0   True   True
1  False  False
2  False  False

In [7]: df.lt(2).dtypes
Out[7]: 
x    bool
y    bool
dtype: object

In [8]: df.lt(2).dtypes.values
Out[8]: array([dtype('bool'), dtype('bool')], dtype=object)

In [9]: df.lt(2).get_dtype_counts()
Out[9]: 
bool    2
dtype: int64

In [10]: Series([2], ['bool'])
Out[10]: 
bool    2
dtype: int64

res = f(const)
c = getattr(res, col)
self.assertEqual(c.dtypes, bool)

def test_dti_tz_convert_to_utc(self):
base = pd.DatetimeIndex(['2011-01-01', '2011-01-02',
'2011-01-03'], tz='UTC')
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,19 @@ def test_comparison_flex_alignment_fill(self):
exp = pd.Series([True, True, False, False], index=list('abcd'))
assert_series_equal(left.gt(right, fill_value=0), exp)

def test_return_dtypes_bool_op_costant(self):
# gh15115
s = pd.Series([1, 3, 2], index=range(3))
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
f = getattr(s, op)
self.assertFalse(f(2).dtypes, np.dtype('bool'))
Copy link
Contributor

Choose a reason for hiding this comment

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

same type of comparison here and below


# empty Series
empty = s.iloc[:0]
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
f = getattr(empty, op)
self.assertFalse(f(2).dtypes, np.dtype('bool'))

def test_operators_bitwise(self):
# GH 9016: support bitwise op for integer types
index = list('bca')
Expand Down