5555 have_pandas_categorical_dtype = (_pandas_is_categorical_dtype
5656 is not None )
5757
58+
5859# Passes through Series and DataFrames, call np.asarray() on everything else
5960def asarray_or_pandas (a , copy = False , dtype = None , subok = False ):
6061 if have_pandas :
@@ -68,10 +69,17 @@ def asarray_or_pandas(a, copy=False, dtype=None, subok=False):
6869 return a .__class__ (a , copy = copy , dtype = dtype , ** extra_args )
6970 return np .array (a , copy = copy , dtype = dtype , subok = subok )
7071
72+
7173def test_asarray_or_pandas ():
74+ import warnings
7275 assert type (asarray_or_pandas ([1 , 2 , 3 ])) is np .ndarray
73- assert type (asarray_or_pandas (np .matrix ([[1 , 2 , 3 ]]))) is np .ndarray
74- assert type (asarray_or_pandas (np .matrix ([[1 , 2 , 3 ]]), subok = True )) is np .matrix
76+ with warnings .catch_warnings () as w :
77+ warnings .filterwarnings ('ignore' , 'the matrix subclass' ,
78+ PendingDeprecationWarning )
79+ assert type (asarray_or_pandas (np .matrix ([[1 , 2 , 3 ]]))) is np .ndarray
80+ assert type (asarray_or_pandas (
81+ np .matrix ([[1 , 2 , 3 ]]), subok = True )) is np .matrix
82+ assert w is None
7583 a = np .array ([1 , 2 , 3 ])
7684 assert asarray_or_pandas (a ) is a
7785 a_copy = asarray_or_pandas (a , copy = True )
@@ -147,7 +155,7 @@ def test_asarray_or_pandas():
147155# instead of rows. It also converts ndarray subclasses into basic ndarrays,
148156# which makes it easier to guarantee correctness. However, there are many
149157# places in the code where we want to preserve pandas indexing information if
150- # present, so there is also an option
158+ # present, so there is also an option
151159def atleast_2d_column_default (a , preserve_pandas = False ):
152160 if preserve_pandas and have_pandas :
153161 if isinstance (a , pandas .Series ):
@@ -162,7 +170,9 @@ def atleast_2d_column_default(a, preserve_pandas=False):
162170 assert a .ndim >= 2
163171 return a
164172
173+
165174def test_atleast_2d_column_default ():
175+ import warnings
166176 assert np .all (atleast_2d_column_default ([1 , 2 , 3 ]) == [[1 ], [2 ], [3 ]])
167177
168178 assert atleast_2d_column_default (1 ).shape == (1 , 1 )
@@ -173,7 +183,11 @@ def test_atleast_2d_column_default():
173183 assert atleast_2d_column_default ([1 , 2 , 3 ]).shape == (3 , 1 )
174184 assert atleast_2d_column_default ([[1 ], [2 ], [3 ]]).shape == (3 , 1 )
175185
176- assert type (atleast_2d_column_default (np .matrix (1 ))) == np .ndarray
186+ with warnings .catch_warnings () as w :
187+ warnings .filterwarnings ('ignore' , 'the matrix subclass' ,
188+ PendingDeprecationWarning )
189+ assert type (atleast_2d_column_default (np .matrix (1 ))) == np .ndarray
190+ assert w is None
177191
178192 global have_pandas
179193 if have_pandas :
@@ -187,18 +201,21 @@ def test_atleast_2d_column_default():
187201 assert (type (atleast_2d_column_default (pandas .DataFrame ([[1 ], [2 ]]),
188202 preserve_pandas = True ))
189203 == pandas .DataFrame )
190- s = pandas .Series ([10 , 11 ,12 ], name = "hi" , index = ["a" , "b" , "c" ])
204+ s = pandas .Series ([10 , 11 , 12 ], name = "hi" , index = ["a" , "b" , "c" ])
191205 df = atleast_2d_column_default (s , preserve_pandas = True )
192206 assert isinstance (df , pandas .DataFrame )
193207 assert np .all (df .columns == ["hi" ])
194208 assert np .all (df .index == ["a" , "b" , "c" ])
195- assert (type (atleast_2d_column_default (np .matrix (1 ),
196- preserve_pandas = True ))
197- == np .ndarray )
198- assert (type (atleast_2d_column_default ([1 , 2 , 3 ],
199- preserve_pandas = True ))
209+ with warnings .catch_warnings () as w :
210+ warnings .filterwarnings ('ignore' , 'the matrix subclass' ,
211+ PendingDeprecationWarning )
212+ assert (type (atleast_2d_column_default (np .matrix (1 ),
213+ preserve_pandas = True ))
214+ == np .ndarray )
215+ assert w is None
216+ assert (type (atleast_2d_column_default ([1 , 2 , 3 ], preserve_pandas = True ))
200217 == np .ndarray )
201-
218+
202219 if have_pandas :
203220 had_pandas = have_pandas
204221 try :
@@ -367,15 +384,15 @@ def test_PushbackAdapter():
367384# The IPython pretty-printer gives very nice output that is difficult to get
368385# otherwise, e.g., look how much more readable this is than if it were all
369386# smooshed onto one line:
370- #
387+ #
371388# ModelDesc(input_code='y ~ x*asdf',
372389# lhs_terms=[Term([EvalFactor('y')])],
373390# rhs_terms=[Term([]),
374391# Term([EvalFactor('x')]),
375392# Term([EvalFactor('asdf')]),
376393# Term([EvalFactor('x'), EvalFactor('asdf')])],
377394# )
378- #
395+ #
379396# But, we don't want to assume it always exists; nor do we want to be
380397# re-writing every repr function twice, once for regular repr and once for
381398# the pretty printer. So, here's an ugly fallback implementation that can be
@@ -562,7 +579,7 @@ def test_safe_isnan():
562579 assert not safe_isnan (None )
563580 # raw isnan raises a *different* error for strings than for objects:
564581 assert not safe_isnan ("asdf" )
565-
582+
566583def iterable (obj ):
567584 try :
568585 iter (obj )
0 commit comments