Skip to content

Commit d923c91

Browse files
author
y-p
committed
CLN: create mode.use_inf_as_null, mode.sim_interactive options
1 parent b15c61b commit d923c91

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

pandas/core/common.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def isnull_old(obj):
9494
else:
9595
return obj is None
9696

97-
def use_inf_as_null(flag):
98-
'''
97+
def _use_inf_as_null(key):
98+
'''Option change callback for null/inf behaviour
9999
Choose which replacement for numpy.isnan / -numpy.isfinite is used.
100100
101101
Parameters
@@ -113,6 +113,7 @@ def use_inf_as_null(flag):
113113
* http://stackoverflow.com/questions/4859217/
114114
programmatically-creating-variables-in-python/4859312#4859312
115115
'''
116+
flag = get_option(key)
116117
if flag == True:
117118
globals()['isnull'] = isnull_old
118119
else:
@@ -1179,7 +1180,7 @@ def in_interactive_session():
11791180
returns True if running under python/ipython interactive shell
11801181
"""
11811182
import __main__ as main
1182-
return not hasattr(main, '__file__') or get_option('test.interactive')
1183+
return not hasattr(main, '__file__') or get_option('mode.sim_interactive')
11831184

11841185
def in_qtconsole():
11851186
"""

pandas/core/config_init.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,27 @@
139139
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
140140
cf.register_option('line_width', 80, pc_line_width_doc)
141141

142-
tc_interactive_doc="""
142+
tc_sim_interactive_doc="""
143143
: boolean
144144
Default False
145145
Whether to simulate interactive mode for purposes of testing
146146
"""
147-
with cf.config_prefix('test'):
148-
cf.register_option('interactive', False, tc_interactive_doc)
147+
with cf.config_prefix('mode'):
148+
cf.register_option('sim_interactive', False, tc_sim_interactive_doc)
149+
150+
use_inf_as_null_doc="""
151+
: boolean
152+
True means treat None, NaN, INF, -INF as null (old way),
153+
False means None and NaN are null, but INF, -INF are not null
154+
(new way).
155+
"""
156+
157+
# we don't want to start importing evrything at the global context level
158+
# or we'll hit circular deps.
159+
def use_inf_as_null_cb(key):
160+
from pandas.core.common import _use_inf_as_null
161+
_use_inf_as_null(key)
162+
163+
with cf.config_prefix('mode'):
164+
cf.register_option('use_inf_as_null', False, use_inf_as_null_doc,
165+
cb=use_inf_as_null_cb)

pandas/tests/test_common.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import unittest
66

77
from pandas import Series, DataFrame, date_range, DatetimeIndex
8-
from pandas.core.common import notnull, isnull, use_inf_as_null
8+
from pandas.core.common import notnull, isnull
99
import pandas.core.common as com
1010
import pandas.util.testing as tm
11+
import pandas.core.config as cf
1112

1213
import numpy as np
1314

@@ -29,15 +30,15 @@ def test_notnull():
2930
assert not notnull(None)
3031
assert not notnull(np.NaN)
3132

32-
use_inf_as_null(False)
33+
cf.set_option("mode.use_inf_as_null",False)
3334
assert notnull(np.inf)
3435
assert notnull(-np.inf)
3536

36-
use_inf_as_null(True)
37+
cf.set_option("mode.use_inf_as_null",True)
3738
assert not notnull(np.inf)
3839
assert not notnull(-np.inf)
3940

40-
use_inf_as_null(False)
41+
cf.set_option("mode.use_inf_as_null",False)
4142

4243
float_series = Series(np.random.randn(5))
4344
obj_series = Series(np.random.randn(5), dtype=object)

pandas/tests/test_format.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ def test_repr_should_return_str (self):
112112
self.assertTrue(type(df.__repr__() == str)) # both py2 / 3
113113

114114
def test_repr_no_backslash(self):
115-
pd.set_option('test.interactive', True)
115+
pd.set_option('mode.sim_interactive', True)
116116
df = DataFrame(np.random.randn(10, 4))
117117

118118
self.assertTrue('\\' not in repr(df))
119-
pd.reset_option('test.interactive')
119+
pd.reset_option('mode.sim_interactive')
120120

121121
def test_to_string_repr_unicode(self):
122122
buf = StringIO()
@@ -409,7 +409,7 @@ def test_frame_info_encoding(self):
409409
fmt.set_printoptions(max_rows=200)
410410

411411
def test_wide_repr(self):
412-
set_option('test.interactive', True)
412+
set_option('mode.sim_interactive', True)
413413
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
414414
df = DataFrame([col(20, 25) for _ in range(10)])
415415
set_option('print.expand_frame_repr', False)
@@ -423,19 +423,19 @@ def test_wide_repr(self):
423423
self.assert_(len(wider_repr) < len(wide_repr))
424424

425425
reset_option('print.expand_frame_repr')
426-
set_option('test.interactive', False)
426+
set_option('mode.sim_interactive', False)
427427
set_option('print.line_width', 80)
428428

429429
def test_wide_repr_wide_columns(self):
430-
set_option('test.interactive', True)
430+
set_option('mode.sim_interactive', True)
431431
df = DataFrame(randn(5, 3), columns=['a' * 90, 'b' * 90, 'c' * 90])
432432
rep_str = repr(df)
433433

434434
self.assert_(len(rep_str.splitlines()) == 20)
435-
reset_option('test.interactive')
435+
reset_option('mode.sim_interactive')
436436

437437
def test_wide_repr_named(self):
438-
set_option('test.interactive', True)
438+
set_option('mode.sim_interactive', True)
439439
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
440440
df = DataFrame([col(20, 25) for _ in range(10)])
441441
df.index.name = 'DataFrame Index'
@@ -454,11 +454,11 @@ def test_wide_repr_named(self):
454454
self.assert_('DataFrame Index' in line)
455455

456456
reset_option('print.expand_frame_repr')
457-
set_option('test.interactive', False)
457+
set_option('mode.sim_interactive', False)
458458
set_option('print.line_width', 80)
459459

460460
def test_wide_repr_multiindex(self):
461-
set_option('test.interactive', True)
461+
set_option('mode.sim_interactive', True)
462462
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
463463
midx = pandas.MultiIndex.from_arrays([np.array(col(10, 5)),
464464
np.array(col(10, 5))])
@@ -479,11 +479,11 @@ def test_wide_repr_multiindex(self):
479479
self.assert_('Level 0 Level 1' in line)
480480

481481
reset_option('print.expand_frame_repr')
482-
set_option('test.interactive', False)
482+
set_option('mode.sim_interactive', False)
483483
set_option('print.line_width', 80)
484484

485485
def test_wide_repr_multiindex_cols(self):
486-
set_option('test.interactive', True)
486+
set_option('mode.sim_interactive', True)
487487
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
488488
midx = pandas.MultiIndex.from_arrays([np.array(col(10, 5)),
489489
np.array(col(10, 5))])
@@ -505,11 +505,11 @@ def test_wide_repr_multiindex_cols(self):
505505
self.assert_(len(wide_repr.splitlines()) == 14 * 10 - 1)
506506

507507
reset_option('print.expand_frame_repr')
508-
set_option('test.interactive', False)
508+
set_option('mode.sim_interactive', False)
509509
set_option('print.line_width', 80)
510510

511511
def test_wide_repr_unicode(self):
512-
set_option('test.interactive', True)
512+
set_option('mode.sim_interactive', True)
513513
col = lambda l, k: [tm.randu(k) for _ in xrange(l)]
514514
df = DataFrame([col(20, 25) for _ in range(10)])
515515
set_option('print.expand_frame_repr', False)
@@ -523,18 +523,18 @@ def test_wide_repr_unicode(self):
523523
self.assert_(len(wider_repr) < len(wide_repr))
524524

525525
reset_option('print.expand_frame_repr')
526-
set_option('test.interactive', False)
526+
set_option('mode.sim_interactive', False)
527527
set_option('print.line_width', 80)
528528

529529
def test_wide_repr_wide_long_columns(self):
530-
set_option('test.interactive', True)
530+
set_option('mode.sim_interactive', True)
531531

532532
df = DataFrame({'a': ['a'*30, 'b'*30], 'b': ['c'*70, 'd'*80]})
533533

534534
result = repr(df)
535535
self.assertTrue('ccccc' in result)
536536
self.assertTrue('ddddd' in result)
537-
set_option('test.interactive', False)
537+
set_option('mode.sim_interactive', False)
538538

539539
def test_to_string(self):
540540
from pandas import read_table

0 commit comments

Comments
 (0)