Skip to content

Commit 4515be9

Browse files
Giacomo Ferronijreback
Giacomo Ferroni
authored andcommitted
BUG: Comparisons result in different dtypes for empty DataFrames #15077
closes #15077 Author: Giacomo Ferroni <[email protected]> Author: Giacomo Ferroni <[email protected]> Author: mralgos <[email protected]> Closes #15115 from mralgos/gh15077 and squashes the following commits: a5ca359 [mralgos] Merge branch 'master' into gh15077 dc0803b [Giacomo Ferroni] Merge branch 'master' into gh15077 b2f2d1e [Giacomo Ferroni] Merge branch 'gh15077' of https://github.com/mralgos/pandas into gh15077 fcbcb5b [Giacomo Ferroni] Apply review changes 9723c5d [Giacomo Ferroni] Merge branch 'master' into gh15077 eb7d9fd [Giacomo Ferroni] Delete blank lines 28437bb [Giacomo Ferroni] Check for bool dtype return added also for Series. Minor update to whatsnew 19296f1 [Giacomo Ferroni] Added test for gh15077 (cf. gh15115) and whatsnew note ea11867 [Giacomo Ferroni] [gh15077] Bugfix
1 parent aa361e5 commit 4515be9

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,10 @@ Bug Fixes
437437

438438
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
439439
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)
440-
440+
- Incorrect dtyped ``Series`` was returned by comparison methods (e.g., ``lt``, ``gt``, ...) against a constant for an empty ``DataFrame`` (:issue:`15077`)
441441
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
442442

443+
443444
- Bug in ``.read_json()`` for Python 2 where ``lines=True`` and contents contain non-ascii unicode characters (:issue:`15132`)
444445

445446
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)

pandas/core/frame.py

-3
Original file line numberDiff line numberDiff line change
@@ -3595,9 +3595,6 @@ def _combine_match_columns(self, other, func, level=None, fill_value=None):
35953595
return self._constructor(new_data)
35963596

35973597
def _combine_const(self, other, func, raise_on_error=True):
3598-
if self.empty:
3599-
return self
3600-
36013598
new_data = self._data.eval(func=func, other=other,
36023599
raise_on_error=raise_on_error)
36033600
return self._constructor(new_data)

pandas/tests/frame/test_operators.py

+16
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,22 @@ def _test_seq(df, idx_ser, col_ser):
671671
exp = DataFrame({'col': [False, True, False]})
672672
assert_frame_equal(result, exp)
673673

674+
def test_return_dtypes_bool_op_costant(self):
675+
# GH15077
676+
df = DataFrame({'x': [1, 2, 3], 'y': [1., 2., 3.]})
677+
const = 2
678+
679+
# not empty DataFrame
680+
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
681+
result = getattr(df, op)(const).get_dtype_counts()
682+
self.assert_series_equal(result, Series([2], ['bool']))
683+
684+
# empty DataFrame
685+
empty = df.iloc[:0]
686+
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
687+
result = getattr(empty, op)(const).get_dtype_counts()
688+
self.assert_series_equal(result, Series([2], ['bool']))
689+
674690
def test_dti_tz_convert_to_utc(self):
675691
base = pd.DatetimeIndex(['2011-01-01', '2011-01-02',
676692
'2011-01-03'], tz='UTC')

pandas/tests/series/test_operators.py

+14
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,20 @@ def test_comparison_flex_alignment_fill(self):
12941294
exp = pd.Series([True, True, False, False], index=list('abcd'))
12951295
assert_series_equal(left.gt(right, fill_value=0), exp)
12961296

1297+
def test_return_dtypes_bool_op_costant(self):
1298+
# gh15115
1299+
s = pd.Series([1, 3, 2], index=range(3))
1300+
const = 2
1301+
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
1302+
result = getattr(s, op)(const).get_dtype_counts()
1303+
self.assert_series_equal(result, Series([1], ['bool']))
1304+
1305+
# empty Series
1306+
empty = s.iloc[:0]
1307+
for op in ['eq', 'ne', 'gt', 'lt', 'ge', 'le']:
1308+
result = getattr(empty, op)(const).get_dtype_counts()
1309+
self.assert_series_equal(result, Series([1], ['bool']))
1310+
12971311
def test_operators_bitwise(self):
12981312
# GH 9016: support bitwise op for integer types
12991313
index = list('bca')

0 commit comments

Comments
 (0)