From c5c44e85df1e3a55fd1449f2b46c3b16298e5f37 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 16 Oct 2015 19:35:19 -0500 Subject: [PATCH] PERF: fast inf checking in to_excel --- asv_bench/asv.conf.json | 1 + doc/source/whatsnew/v0.17.1.txt | 2 ++ pandas/core/format.py | 4 ++-- pandas/lib.pyx | 12 ++++++++++++ pandas/tests/test_lib.py | 15 ++++++++++++++- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json index dcea59545aae3..6a739873a032f 100644 --- a/asv_bench/asv.conf.json +++ b/asv_bench/asv.conf.json @@ -43,6 +43,7 @@ "numexpr": [], "pytables": [], "openpyxl": [], + "xlsxwriter": [], "xlrd": [], "xlwt": [] }, diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index e9614ec4f2290..05c99e38e8511 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -62,6 +62,8 @@ Performance Improvements - Release the GIL on most datetime field operations (e.g. ``DatetimeIndex.year``, ``Series.dt.year``), normalization, and conversion to and from ``Period``, ``DatetimeIndex.to_period`` and ``PeriodIndex.to_timestamp`` (:issue:`11263`) +- Improved performance to ``to_excel`` (:issue:`11352`) + .. _whatsnew_0171.bug_fixes: Bug Fixes diff --git a/pandas/core/format.py b/pandas/core/format.py index e4aa1eac248d5..4a3b1d02c9422 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -1708,9 +1708,9 @@ def _format_value(self, val): if lib.checknull(val): val = self.na_rep elif com.is_float(val): - if np.isposinf(val): + if lib.isposinf_scalar(val): val = self.inf_rep - elif np.isneginf(val): + elif lib.isneginf_scalar(val): val = '-%s' % self.inf_rep elif self.float_format is not None: val = float(self.float_format % val) diff --git a/pandas/lib.pyx b/pandas/lib.pyx index 2b4974155d44c..74842d9a165fe 100644 --- a/pandas/lib.pyx +++ b/pandas/lib.pyx @@ -269,6 +269,18 @@ cpdef checknull_old(object val): else: return util._checknull(val) +cpdef isposinf_scalar(object val): + if util.is_float_object(val) and val == INF: + return True + else: + return False + +cpdef isneginf_scalar(object val): + if util.is_float_object(val) and val == NEGINF: + return True + else: + return False + def isscalar(object val): """ Return True if given value is scalar. diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index cfc98f5c20360..a24f71482c404 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -161,6 +161,19 @@ def test_maybe_indices_to_slice_middle(self): self.assert_numpy_array_equal(maybe_slice, indices) self.assert_numpy_array_equal(target[indices], target[maybe_slice]) + def test_isinf_scalar(self): + #GH 11352 + self.assertTrue(lib.isposinf_scalar(float('inf'))) + self.assertTrue(lib.isposinf_scalar(np.inf)) + self.assertFalse(lib.isposinf_scalar(-np.inf)) + self.assertFalse(lib.isposinf_scalar(1)) + self.assertFalse(lib.isposinf_scalar('a')) + + self.assertTrue(lib.isneginf_scalar(float('-inf'))) + self.assertTrue(lib.isneginf_scalar(-np.inf)) + self.assertFalse(lib.isneginf_scalar(np.inf)) + self.assertFalse(lib.isneginf_scalar(1)) + self.assertFalse(lib.isneginf_scalar('a')) class Testisscalar(tm.TestCase): @@ -232,4 +245,4 @@ def test_lisscalar_pandas_containers(self): import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], - exit=False) \ No newline at end of file + exit=False)