Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/statsmodels/compat/pandas.py : 35%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
2from distutils.version import LooseVersion
4import numpy as np
5import pandas as pd
6from pandas.util._decorators import deprecate_kwarg, Appender, Substitution
8__all__ = ['assert_frame_equal', 'assert_index_equal', 'assert_series_equal',
9 'data_klasses', 'frequencies', 'is_numeric_dtype', 'testing',
10 'cache_readonly', 'deprecate_kwarg', 'Appender', 'Substitution',
11 'make_dataframe', 'assert_equal']
13version = LooseVersion(pd.__version__)
14pandas_lt_25_0 = version < LooseVersion('0.25.0')
15pandas_gte_23_0 = version >= LooseVersion('0.23.0')
17try:
18 from pandas.api.types import is_numeric_dtype
19except ImportError:
20 from pandas.core.common import is_numeric_dtype
22try:
23 from pandas.tseries import offsets as frequencies
24except ImportError:
25 from pandas.tseries import frequencies
27data_klasses = (pd.Series, pd.DataFrame)
28if pandas_lt_25_0:
29 data_klasses += (pd.Panel,)
31try:
32 import pandas.testing as testing
33except ImportError:
34 import pandas.util.testing as testing
36assert_frame_equal = testing.assert_frame_equal
37assert_index_equal = testing.assert_index_equal
38assert_series_equal = testing.assert_series_equal
40try:
41 from pandas.testing import assert_equal
42except ImportError:
43 def assert_equal(left, right):
44 """
45 pandas >= 0.24.0 has `pandas.testing.assert_equal` that works for any
46 of Index, Series, and DataFrame inputs. Until statsmodels requirements
47 catch up to that, we implement a version of that here.
49 Parameters
50 ----------
51 left : pd.Index, pd.Series, or pd.DataFrame
52 right : object
54 Raises
55 ------
56 AssertionError
57 """
58 if isinstance(left, pd.Index):
59 assert_index_equal(left, right)
60 elif isinstance(left, pd.Series):
61 assert_series_equal(left, right)
62 elif isinstance(left, pd.DataFrame):
63 assert_frame_equal(left, right)
64 else:
65 raise TypeError(type(left))
67if pandas_gte_23_0:
68 from pandas.util._decorators import cache_readonly
69else:
70 class CachedProperty(object):
72 def __init__(self, func):
73 self.func = func
74 self.name = func.__name__
75 self.__doc__ = getattr(func, '__doc__', None)
77 def __get__(self, obj, typ):
78 if obj is None:
79 # accessed on the class, not the instance
80 return self
82 # Get the cache or set a default one if needed
83 cache = getattr(obj, '_cache', None)
84 if cache is None:
85 try:
86 cache = obj._cache = {}
87 except (AttributeError):
88 return self
90 if self.name in cache:
91 # not necessary to Py_INCREF
92 val = cache[self.name]
93 else:
94 val = self.func(obj)
95 cache[self.name] = val
96 return val
98 def __set__(self, obj, value):
99 raise AttributeError("Can't set attribute")
101 cache_readonly = CachedProperty
103try:
104 from pandas._testing import makeDataFrame as make_dataframe
105except ImportError:
106 import string
108 def rands_array(nchars, size, dtype="O"):
109 """
110 Generate an array of byte strings.
111 """
112 rands_chars = np.array(list(string.ascii_letters + string.digits),
113 dtype=(np.str_, 1))
114 retval = (np.random.choice(rands_chars, size=nchars * np.prod(size))
115 .view((np.str_, nchars))
116 .reshape(size))
117 if dtype is None:
118 return retval
119 else:
120 return retval.astype(dtype)
122 def make_dataframe():
123 """
124 Simple verion of pandas._testing.makeDataFrame
125 """
126 n = 30
127 k = 4
128 index = pd.Index(rands_array(nchars=10, size=n), name=None)
129 data = {c: pd.Series(np.random.randn(n), index=index)
130 for c in string.ascii_uppercase[:k]}
132 return pd.DataFrame(data)