55
55
have_pandas_categorical_dtype = (_pandas_is_categorical_dtype
56
56
is not None )
57
57
58
+
58
59
# Passes through Series and DataFrames, call np.asarray() on everything else
59
60
def asarray_or_pandas (a , copy = False , dtype = None , subok = False ):
60
61
if have_pandas :
@@ -68,10 +69,17 @@ def asarray_or_pandas(a, copy=False, dtype=None, subok=False):
68
69
return a .__class__ (a , copy = copy , dtype = dtype , ** extra_args )
69
70
return np .array (a , copy = copy , dtype = dtype , subok = subok )
70
71
72
+
71
73
def test_asarray_or_pandas ():
74
+ import warnings
72
75
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
75
83
a = np .array ([1 , 2 , 3 ])
76
84
assert asarray_or_pandas (a ) is a
77
85
a_copy = asarray_or_pandas (a , copy = True )
@@ -147,7 +155,7 @@ def test_asarray_or_pandas():
147
155
# instead of rows. It also converts ndarray subclasses into basic ndarrays,
148
156
# which makes it easier to guarantee correctness. However, there are many
149
157
# 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
151
159
def atleast_2d_column_default (a , preserve_pandas = False ):
152
160
if preserve_pandas and have_pandas :
153
161
if isinstance (a , pandas .Series ):
@@ -162,7 +170,9 @@ def atleast_2d_column_default(a, preserve_pandas=False):
162
170
assert a .ndim >= 2
163
171
return a
164
172
173
+
165
174
def test_atleast_2d_column_default ():
175
+ import warnings
166
176
assert np .all (atleast_2d_column_default ([1 , 2 , 3 ]) == [[1 ], [2 ], [3 ]])
167
177
168
178
assert atleast_2d_column_default (1 ).shape == (1 , 1 )
@@ -173,7 +183,11 @@ def test_atleast_2d_column_default():
173
183
assert atleast_2d_column_default ([1 , 2 , 3 ]).shape == (3 , 1 )
174
184
assert atleast_2d_column_default ([[1 ], [2 ], [3 ]]).shape == (3 , 1 )
175
185
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
177
191
178
192
global have_pandas
179
193
if have_pandas :
@@ -187,18 +201,21 @@ def test_atleast_2d_column_default():
187
201
assert (type (atleast_2d_column_default (pandas .DataFrame ([[1 ], [2 ]]),
188
202
preserve_pandas = True ))
189
203
== 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" ])
191
205
df = atleast_2d_column_default (s , preserve_pandas = True )
192
206
assert isinstance (df , pandas .DataFrame )
193
207
assert np .all (df .columns == ["hi" ])
194
208
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 ))
200
217
== np .ndarray )
201
-
218
+
202
219
if have_pandas :
203
220
had_pandas = have_pandas
204
221
try :
@@ -367,15 +384,15 @@ def test_PushbackAdapter():
367
384
# The IPython pretty-printer gives very nice output that is difficult to get
368
385
# otherwise, e.g., look how much more readable this is than if it were all
369
386
# smooshed onto one line:
370
- #
387
+ #
371
388
# ModelDesc(input_code='y ~ x*asdf',
372
389
# lhs_terms=[Term([EvalFactor('y')])],
373
390
# rhs_terms=[Term([]),
374
391
# Term([EvalFactor('x')]),
375
392
# Term([EvalFactor('asdf')]),
376
393
# Term([EvalFactor('x'), EvalFactor('asdf')])],
377
394
# )
378
- #
395
+ #
379
396
# But, we don't want to assume it always exists; nor do we want to be
380
397
# re-writing every repr function twice, once for regular repr and once for
381
398
# the pretty printer. So, here's an ugly fallback implementation that can be
@@ -562,7 +579,7 @@ def test_safe_isnan():
562
579
assert not safe_isnan (None )
563
580
# raw isnan raises a *different* error for strings than for objects:
564
581
assert not safe_isnan ("asdf" )
565
-
582
+
566
583
def iterable (obj ):
567
584
try :
568
585
iter (obj )
0 commit comments