Skip to content

Commit 166f907

Browse files
committed
Fixed _format_labels in tile.py for np.inf.
Since the fractional part of np.inf is 0.0 it is enough to change the formatter string in this case to the (nearly) equivalent "%.0f".
1 parent a11e143 commit 166f907

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

doc/source/basics.rst

+6
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,12 @@ normally distributed data into equal-size quartiles like so:
505505
factor
506506
value_counts(factor)
507507
508+
We can also pass infinite values to define the bins:
509+
.. ipython:: python
510+
511+
arr = np.random.randn(20)
512+
factor = cut(arr, [-np.inf, 0, np.inf])
513+
factor
508514
509515
.. _basics.apply:
510516

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ Bug Fixes
462462
- Fixed ``copy()`` to shallow copy axes/indices as well and thereby keep
463463
separate metadata. (:issue:`4202`, :issue:`4830`)
464464
- Fixed skiprows option in Python parser for read_csv (:issue:`4382`)
465+
- Fixed bug preventing ``cut`` from working with ``np.inf`` levels without
466+
explicitly passing labels (:issue:`3415`)
465467

466468
pandas 0.12.0
467469
-------------

pandas/tools/tests/test_tile.py

+16
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ def test_na_handling(self):
114114
ex_result = np.where(com.isnull(arr), np.nan, result)
115115
tm.assert_almost_equal(result, ex_result)
116116

117+
def test_inf_handling(self):
118+
data = np.arange(6)
119+
data_ser = Series(data)
120+
121+
result = cut(data, [-np.inf, 2, 4, np.inf])
122+
result_ser = cut(data_ser, [-np.inf, 2, 4, np.inf])
123+
124+
ex_levels = ['(-inf, 2]', '(2, 4]', '(4, inf]']
125+
126+
np.testing.assert_array_equal(result.levels, ex_levels)
127+
np.testing.assert_array_equal(result_ser.levels, ex_levels)
128+
self.assertEquals(result[5], '(4, inf]')
129+
self.assertEquals(result[0], '(-inf, 2]')
130+
self.assertEquals(result_ser[5], '(4, inf]')
131+
self.assertEquals(result_ser[0], '(-inf, 2]')
132+
117133
def test_qcut(self):
118134
arr = np.random.randn(1000)
119135

pandas/tools/tile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def _format_label(x, precision=3):
245245
else: # pragma: no cover
246246
return sgn + '.'.join(('%d' % whole, val))
247247
else:
248-
return sgn + '%d' % whole
248+
return sgn + '%0.f' % whole
249249
else:
250250
return str(x)
251251

0 commit comments

Comments
 (0)