Python pandas.option_context() Examples
The following are 30
code examples of pandas.option_context().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas
, or try the search function
.
Example #1
Source File: test_format.py From recruit with Apache License 2.0 | 7 votes |
def test_wide_repr(self): with option_context('mode.sim_interactive', True, 'display.show_dimensions', True, 'display.max_columns', 20): max_cols = get_option('display.max_columns') df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1))) set_option('display.expand_frame_repr', False) rep_str = repr(df) assert "10 rows x {c} columns".format(c=max_cols - 1) in rep_str set_option('display.expand_frame_repr', True) wide_repr = repr(df) assert rep_str != wide_repr with option_context('display.width', 120): wider_repr = repr(df) assert len(wider_repr) < len(wide_repr) reset_option('display.expand_frame_repr')
Example #2
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_wide_repr_multiindex_cols(self): with option_context('mode.sim_interactive', True, 'display.max_columns', 20): max_cols = get_option('display.max_columns') midx = MultiIndex.from_arrays(tm.rands_array(5, size=(2, 10))) mcols = MultiIndex.from_arrays( tm.rands_array(3, size=(2, max_cols - 1))) df = DataFrame(tm.rands_array(25, (10, max_cols - 1)), index=midx, columns=mcols) df.index.names = ['Level 0', 'Level 1'] set_option('display.expand_frame_repr', False) rep_str = repr(df) set_option('display.expand_frame_repr', True) wide_repr = repr(df) assert rep_str != wide_repr with option_context('display.width', 150, 'display.max_columns', 20): wider_repr = repr(df) assert len(wider_repr) < len(wide_repr) reset_option('display.expand_frame_repr')
Example #3
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_sparse_int(self): # GH 13110 s = pd.SparseSeries([0, 1, 0, 0, 1, 0], fill_value=False) result = repr(s) dtype = '' if use_32bit_repr else ', dtype=int32' exp = ("0 0\n1 1\n2 0\n3 0\n4 1\n" "5 0\ndtype: Sparse[int64, False]\nBlockIndex\n" "Block locations: array([1, 4]{0})\n" "Block lengths: array([1, 1]{0})".format(dtype)) assert result == exp with option_context("display.max_rows", 3, "display.show_dimensions", False): result = repr(s) exp = ("0 0\n ..\n5 0\n" "dtype: Sparse[int64, False]\nBlockIndex\n" "Block locations: array([1, 4]{0})\n" "Block lengths: array([1, 1]{0})".format(dtype)) assert result == exp
Example #4
Source File: test_reductions.py From recruit with Apache License 2.0 | 6 votes |
def test_idxminmax_with_inf(self): # For numeric data with NA and Inf (GH #13595) s = pd.Series([0, -np.inf, np.inf, np.nan]) assert s.idxmin() == 1 assert np.isnan(s.idxmin(skipna=False)) assert s.idxmax() == 2 assert np.isnan(s.idxmax(skipna=False)) # Using old-style behavior that treats floating point nan, -inf, and # +inf as missing with pd.option_context('mode.use_inf_as_na', True): assert s.idxmin() == 0 assert np.isnan(s.idxmin(skipna=False)) assert s.idxmax() == 0 np.isnan(s.idxmax(skipna=False))
Example #5
Source File: test_analytics.py From recruit with Apache License 2.0 | 6 votes |
def test_sem(self, float_frame_with_na, datetime_frame, float_frame, float_string_frame): alt = lambda x: np.std(x, ddof=1) / np.sqrt(len(x)) assert_stat_op_calc('sem', alt, float_frame_with_na) assert_stat_op_api('sem', float_frame, float_string_frame) result = datetime_frame.sem(ddof=4) expected = datetime_frame.apply( lambda x: x.std(ddof=4) / np.sqrt(len(x))) tm.assert_almost_equal(result, expected) arr = np.repeat(np.random.random((1, 1000)), 1000, 0) result = nanops.nansem(arr, axis=0) assert not (result < 0).any() with pd.option_context('use_bottleneck', False): result = nanops.nansem(arr, axis=0) assert not (result < 0).any()
Example #6
Source File: test_analytics.py From recruit with Apache License 2.0 | 6 votes |
def test_var_std(self, float_frame_with_na, datetime_frame, float_frame, float_string_frame): alt = lambda x: np.var(x, ddof=1) assert_stat_op_calc('var', alt, float_frame_with_na) assert_stat_op_api('var', float_frame, float_string_frame) alt = lambda x: np.std(x, ddof=1) assert_stat_op_calc('std', alt, float_frame_with_na) assert_stat_op_api('std', float_frame, float_string_frame) result = datetime_frame.std(ddof=4) expected = datetime_frame.apply(lambda x: x.std(ddof=4)) tm.assert_almost_equal(result, expected) result = datetime_frame.var(ddof=4) expected = datetime_frame.apply(lambda x: x.var(ddof=4)) tm.assert_almost_equal(result, expected) arr = np.repeat(np.random.random((1, 1000)), 1000, 0) result = nanops.nanvar(arr, axis=0) assert not (result < 0).any() with pd.option_context('use_bottleneck', False): result = nanops.nanvar(arr, axis=0) assert not (result < 0).any()
Example #7
Source File: test_repr.py From recruit with Apache License 2.0 | 6 votes |
def test_latex_repr(self): result = r"""\begin{tabular}{ll} \toprule {} & 0 \\ \midrule 0 & $\alpha$ \\ 1 & b \\ 2 & c \\ \bottomrule \end{tabular} """ with option_context('display.latex.escape', False, 'display.latex.repr', True): s = Series([r'$\alpha$', 'b', 'c']) assert result == s._repr_latex_() assert s._repr_latex_() is None
Example #8
Source File: test_repr.py From recruit with Apache License 2.0 | 6 votes |
def test_categorical_repr(self): a = Series(Categorical([1, 2, 3, 4])) exp = u("0 1\n1 2\n2 3\n3 4\n" + "dtype: category\nCategories (4, int64): [1, 2, 3, 4]") assert exp == a.__unicode__() a = Series(Categorical(["a", "b"] * 25)) exp = u("0 a\n1 b\n" + " ..\n" + "48 a\n49 b\n" + "Length: 50, dtype: category\nCategories (2, object): [a, b]") with option_context("display.max_rows", 5): assert exp == repr(a) levs = list("abcdefghijklmnopqrstuvwxyz") a = Series(Categorical(["a", "b"], categories=levs, ordered=True)) exp = u("0 a\n1 b\n" + "dtype: category\n" "Categories (26, object): [a < b < c < d ... w < x < y < z]") assert exp == a.__unicode__()
Example #9
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_sparse_mi_max_row(self): idx = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 0), ('C', 0), ('C', 1), ('C', 2)]) s = pd.Series([1, np.nan, np.nan, 3, np.nan, np.nan], index=idx).to_sparse() result = repr(s) dfm = self.dtype_format_for_platform exp = ("A 0 1.0\n 1 NaN\nB 0 NaN\n" "C 0 3.0\n 1 NaN\n 2 NaN\n" "dtype: Sparse[float64, nan]\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp with option_context("display.max_rows", 3, "display.show_dimensions", False): # GH 13144 result = repr(s) exp = ("A 0 1.0\n ... \nC 2 NaN\n" "dtype: Sparse[float64, nan]\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp
Example #10
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_show_null_counts(self): df = DataFrame(1, columns=range(10), index=range(10)) df.iloc[1, 1] = np.nan def check(null_counts, result): buf = StringIO() df.info(buf=buf, null_counts=null_counts) assert ('non-null' in buf.getvalue()) is result with option_context('display.max_info_rows', 20, 'display.max_info_columns', 20): check(None, True) check(True, True) check(False, False) with option_context('display.max_info_rows', 5, 'display.max_info_columns', 5): check(None, False) check(True, False) check(False, False)
Example #11
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_repr_truncation(self): max_len = 20 with option_context("display.max_colwidth", max_len): df = DataFrame({'A': np.random.randn(10), 'B': [tm.rands(np.random.randint( max_len - 1, max_len + 1)) for i in range(10) ]}) r = repr(df) r = r[r.find('\n') + 1:] adj = fmt._get_adjustment() for line, value in lzip(r.split('\n'), df['B']): if adj.len(value) + 1 > max_len: assert '...' in line else: assert '...' not in line with option_context("display.max_colwidth", 999999): assert '...' not in repr(df) with option_context("display.max_colwidth", max_len + 2): assert '...' not in repr(df)
Example #12
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_expand_frame_repr(self): df_small = DataFrame('hello', [0], [0]) df_wide = DataFrame('hello', [0], lrange(10)) df_tall = DataFrame('hello', lrange(30), lrange(5)) with option_context('mode.sim_interactive', True): with option_context('display.max_columns', 10, 'display.width', 20, 'display.max_rows', 20, 'display.show_dimensions', True): with option_context('display.expand_frame_repr', True): assert not has_truncated_repr(df_small) assert not has_expanded_repr(df_small) assert not has_truncated_repr(df_wide) assert has_expanded_repr(df_wide) assert has_vertically_truncated_repr(df_tall) assert has_expanded_repr(df_tall) with option_context('display.expand_frame_repr', False): assert not has_truncated_repr(df_small) assert not has_expanded_repr(df_small) assert not has_horizontally_truncated_repr(df_wide) assert not has_expanded_repr(df_wide) assert has_vertically_truncated_repr(df_tall) assert not has_expanded_repr(df_tall)
Example #13
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_str_max_colwidth(self): # GH 7856 df = pd.DataFrame([{'a': 'foo', 'b': 'bar', 'c': 'uncomfortably long line with lots of stuff', 'd': 1}, {'a': 'foo', 'b': 'bar', 'c': 'stuff', 'd': 1}]) df.set_index(['a', 'b', 'c']) assert str(df) == ( ' a b c d\n' '0 foo bar uncomfortably long line with lots of stuff 1\n' '1 foo bar stuff 1') with option_context('max_colwidth', 20): assert str(df) == (' a b c d\n' '0 foo bar uncomfortably lo... 1\n' '1 foo bar stuff 1')
Example #14
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_truncate_with_different_dtypes(self): # 11594, 12045 # when truncated the dtypes of the splits can differ # 11594 import datetime s = Series([datetime.datetime(2012, 1, 1)] * 10 + [datetime.datetime(1012, 1, 2)] + [ datetime.datetime(2012, 1, 3)] * 10) with pd.option_context('display.max_rows', 8): result = str(s) assert 'object' in result # 12045 df = DataFrame({'text': ['some words'] + [None] * 9}) with pd.option_context('display.max_rows', 8, 'display.max_columns', 3): result = str(df) assert 'None' in result assert 'NaN' not in result
Example #15
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_wide_repr_multiindex(self): with option_context('mode.sim_interactive', True, 'display.max_columns', 20): midx = MultiIndex.from_arrays(tm.rands_array(5, size=(2, 10))) max_cols = get_option('display.max_columns') df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)), index=midx) df.index.names = ['Level 0', 'Level 1'] set_option('display.expand_frame_repr', False) rep_str = repr(df) set_option('display.expand_frame_repr', True) wide_repr = repr(df) assert rep_str != wide_repr with option_context('display.width', 150): wider_repr = repr(df) assert len(wider_repr) < len(wide_repr) for line in wide_repr.splitlines()[1::13]: assert 'Level 0 Level 1' in line reset_option('display.expand_frame_repr')
Example #16
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_wide_repr_named(self): with option_context('mode.sim_interactive', True, 'display.max_columns', 20): max_cols = get_option('display.max_columns') df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1))) df.index.name = 'DataFrame Index' set_option('display.expand_frame_repr', False) rep_str = repr(df) set_option('display.expand_frame_repr', True) wide_repr = repr(df) assert rep_str != wide_repr with option_context('display.width', 150): wider_repr = repr(df) assert len(wider_repr) < len(wide_repr) for line in wide_repr.splitlines()[1::13]: assert 'DataFrame Index' in line reset_option('display.expand_frame_repr')
Example #17
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_wide_repr_unicode(self): with option_context('mode.sim_interactive', True, 'display.max_columns', 20): max_cols = 20 df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1))) set_option('display.expand_frame_repr', False) rep_str = repr(df) set_option('display.expand_frame_repr', True) wide_repr = repr(df) assert rep_str != wide_repr with option_context('display.width', 150): wider_repr = repr(df) assert len(wider_repr) < len(wide_repr) reset_option('display.expand_frame_repr')
Example #18
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_repr_html_wide_multiindex_cols(self): max_cols = 20 mcols = MultiIndex.from_product([np.arange(max_cols // 2), ['foo', 'bar']], names=['first', 'second']) df = DataFrame(tm.rands_array(25, size=(10, len(mcols))), columns=mcols) reg_repr = df._repr_html_() assert '...' not in reg_repr mcols = MultiIndex.from_product((np.arange(1 + (max_cols // 2)), ['foo', 'bar']), names=['first', 'second']) df = DataFrame(tm.rands_array(25, size=(10, len(mcols))), columns=mcols) with option_context('display.max_rows', 60, 'display.max_columns', 20): assert '...' in df._repr_html_()
Example #19
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_repr_html_long(self): with option_context('display.max_rows', 60): max_rows = get_option('display.max_rows') h = max_rows - 1 df = DataFrame({'A': np.arange(1, 1 + h), 'B': np.arange(41, 41 + h)}) reg_repr = df._repr_html_() assert '..' not in reg_repr assert str(41 + max_rows // 2) in reg_repr h = max_rows + 1 df = DataFrame({'A': np.arange(1, 1 + h), 'B': np.arange(41, 41 + h)}) long_repr = df._repr_html_() assert '..' in long_repr assert str(41 + max_rows // 2) not in long_repr assert u('{h} rows ').format(h=h) in long_repr assert u('2 columns') in long_repr
Example #20
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_repr_html_float(self): with option_context('display.max_rows', 60): max_rows = get_option('display.max_rows') h = max_rows - 1 df = DataFrame({'idx': np.linspace(-10, 10, h), 'A': np.arange(1, 1 + h), 'B': np.arange(41, 41 + h)}).set_index('idx') reg_repr = df._repr_html_() assert '..' not in reg_repr assert '<td>{val}</td>'.format(val=str(40 + h)) in reg_repr h = max_rows + 1 df = DataFrame({'idx': np.linspace(-10, 10, h), 'A': np.arange(1, 1 + h), 'B': np.arange(41, 41 + h)}).set_index('idx') long_repr = df._repr_html_() assert '..' in long_repr assert '<td>{val}</td>'.format(val='31') not in long_repr assert u('{h} rows ').format(h=h) in long_repr assert u('2 columns') in long_repr
Example #21
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_info_repr(self): # GH#21746 For tests inside a terminal (i.e. not CI) we need to detect # the terminal size to ensure that we try to print something "too big" term_width, term_height = get_terminal_size() max_rows = 60 max_cols = 20 + (max(term_width, 80) - 80) // 4 # Long h, w = max_rows + 1, max_cols - 1 df = DataFrame({k: np.arange(1, 1 + h) for k in np.arange(w)}) assert has_vertically_truncated_repr(df) with option_context('display.large_repr', 'info'): assert has_info_repr(df) # Wide h, w = max_rows - 1, max_cols + 1 df = DataFrame({k: np.arange(1, 1 + h) for k in np.arange(w)}) assert has_horizontally_truncated_repr(df) with option_context('display.large_repr', 'info', 'display.max_columns', max_cols): assert has_info_repr(df)
Example #22
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_info_repr_html(self): max_rows = 60 max_cols = 20 # Long h, w = max_rows + 1, max_cols - 1 df = DataFrame({k: np.arange(1, 1 + h) for k in np.arange(w)}) assert r'<class' not in df._repr_html_() with option_context('display.large_repr', 'info'): assert r'<class' in df._repr_html_() # Wide h, w = max_rows - 1, max_cols + 1 df = DataFrame({k: np.arange(1, 1 + h) for k in np.arange(w)}) assert '<class' not in df._repr_html_() with option_context('display.large_repr', 'info', 'display.max_columns', max_cols): assert '<class' in df._repr_html_()
Example #23
Source File: test_format.py From recruit with Apache License 2.0 | 6 votes |
def test_format_explicit(self): test_sers = gen_series_formatting() with option_context("display.max_rows", 4, "display.show_dimensions", False): res = repr(test_sers['onel']) exp = '0 a\n1 a\n ..\n98 a\n99 a\ndtype: object' assert exp == res res = repr(test_sers['twol']) exp = ('0 ab\n1 ab\n ..\n98 ab\n99 ab\ndtype:' ' object') assert exp == res res = repr(test_sers['asc']) exp = ('0 a\n1 ab\n ... \n4 abcde\n5' ' abcdef\ndtype: object') assert exp == res res = repr(test_sers['desc']) exp = ('5 abcdef\n4 abcde\n ... \n1 ab\n0' ' a\ndtype: object') assert exp == res
Example #24
Source File: test_printing.py From recruit with Apache License 2.0 | 6 votes |
def test_publishes(self): df = pd.DataFrame({"A": [1, 2]}) objects = [df['A'], df, df] # dataframe / series expected_keys = [ {'text/plain', 'application/vnd.dataresource+json'}, {'text/plain', 'text/html', 'application/vnd.dataresource+json'}, ] opt = pd.option_context('display.html.table_schema', True) for obj, expected in zip(objects, expected_keys): with opt: formatted = self.display_formatter.format(obj) assert set(formatted[0].keys()) == expected with_latex = pd.option_context('display.latex.repr', True) with opt, with_latex: formatted = self.display_formatter.format(obj) expected = {'text/plain', 'text/html', 'text/latex', 'application/vnd.dataresource+json'} assert set(formatted[0].keys()) == expected
Example #25
Source File: test_printing.py From recruit with Apache License 2.0 | 6 votes |
def test_enable_data_resource_formatter(self): # GH 10491 formatters = self.display_formatter.formatters mimetype = 'application/vnd.dataresource+json' with pd.option_context('display.html.table_schema', True): assert 'application/vnd.dataresource+json' in formatters assert formatters[mimetype].enabled # still there, just disabled assert 'application/vnd.dataresource+json' in formatters assert not formatters[mimetype].enabled # able to re-set with pd.option_context('display.html.table_schema', True): assert 'application/vnd.dataresource+json' in formatters assert formatters[mimetype].enabled # smoke test that it works self.display_formatter.format(cf)
Example #26
Source File: test_parquet.py From recruit with Apache License 2.0 | 6 votes |
def test_options_get_engine(fp, pa): assert isinstance(get_engine('pyarrow'), PyArrowImpl) assert isinstance(get_engine('fastparquet'), FastParquetImpl) with pd.option_context('io.parquet.engine', 'pyarrow'): assert isinstance(get_engine('auto'), PyArrowImpl) assert isinstance(get_engine('pyarrow'), PyArrowImpl) assert isinstance(get_engine('fastparquet'), FastParquetImpl) with pd.option_context('io.parquet.engine', 'fastparquet'): assert isinstance(get_engine('auto'), FastParquetImpl) assert isinstance(get_engine('pyarrow'), PyArrowImpl) assert isinstance(get_engine('fastparquet'), FastParquetImpl) with pd.option_context('io.parquet.engine', 'auto'): assert isinstance(get_engine('auto'), PyArrowImpl) assert isinstance(get_engine('pyarrow'), PyArrowImpl) assert isinstance(get_engine('fastparquet'), FastParquetImpl)
Example #27
Source File: core.py From mars with Apache License 2.0 | 6 votes |
def _repr_html_(self): if len(self._executed_sessions) == 0: # not executed before, fall back to normal repr raise NotImplementedError corner_data = fetch_corner_data( self, session=self._executed_sessions[-1]) buf = StringIO() max_rows = pd.get_option('display.max_rows') if self.shape[0] <= max_rows: buf.write(corner_data._repr_html_()) else: with pd.option_context('display.show_dimensions', False, 'display.max_rows', corner_data.shape[0] - 1): buf.write(corner_data._repr_html_().rstrip().rstrip('</div>')) if pd.get_option('display.show_dimensions'): n_rows, n_cols = self.shape buf.write( "<p>{nrows} rows × {ncols} columns</p>\n".format( nrows=n_rows, ncols=n_cols) ) buf.write('</div>') return buf.getvalue()
Example #28
Source File: test_format.py From vnpy_crypto with MIT License | 6 votes |
def test_sparse_max_row(self): s = pd.Series([1, np.nan, np.nan, 3, np.nan]).to_sparse() result = repr(s) dfm = self.dtype_format_for_platform exp = ("0 1.0\n1 NaN\n2 NaN\n3 3.0\n" "4 NaN\ndtype: float64\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp with option_context("display.max_rows", 3): # GH 10560 result = repr(s) exp = ("0 1.0\n ... \n4 NaN\n" "Length: 5, dtype: float64\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp
Example #29
Source File: test_format.py From vnpy_crypto with MIT License | 6 votes |
def test_sparse_mi_max_row(self): idx = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 0), ('C', 0), ('C', 1), ('C', 2)]) s = pd.Series([1, np.nan, np.nan, 3, np.nan, np.nan], index=idx).to_sparse() result = repr(s) dfm = self.dtype_format_for_platform exp = ("A 0 1.0\n 1 NaN\nB 0 NaN\n" "C 0 3.0\n 1 NaN\n 2 NaN\n" "dtype: float64\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp with option_context("display.max_rows", 3, "display.show_dimensions", False): # GH 13144 result = repr(s) exp = ("A 0 1.0\n ... \nC 2 NaN\n" "dtype: float64\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dfm)) assert result == exp
Example #30
Source File: test_format.py From vnpy_crypto with MIT License | 6 votes |
def test_sparse_bool(self): # GH 13110 s = pd.SparseSeries([True, False, False, True, False, False], fill_value=False) result = repr(s) dtype = '' if use_32bit_repr else ', dtype=int32' exp = ("0 True\n1 False\n2 False\n" "3 True\n4 False\n5 False\n" "dtype: bool\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dtype)) assert result == exp with option_context("display.max_rows", 3): result = repr(s) exp = ("0 True\n ... \n5 False\n" "Length: 6, dtype: bool\nBlockIndex\n" "Block locations: array([0, 3]{0})\n" "Block lengths: array([1, 1]{0})".format(dtype)) assert result == exp