forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_common.py
855 lines (648 loc) · 27.6 KB
/
test_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# -*- coding: utf-8 -*-
import collections
from datetime import datetime, timedelta
import re
import nose
import numpy as np
import pandas as pd
from pandas.tslib import iNaT, NaT
from pandas import (Series, DataFrame, date_range, DatetimeIndex,
TimedeltaIndex, Timestamp, Float64Index)
from pandas import compat
from pandas.compat import range, lrange, lmap, u
from pandas.core.common import notnull, isnull, array_equivalent
import pandas.core.common as com
import pandas.core.convert as convert
import pandas.util.testing as tm
import pandas.core.config as cf
_multiprocess_can_split_ = True
def test_mut_exclusive():
msg = "mutually exclusive arguments: '[ab]' and '[ab]'"
with tm.assertRaisesRegexp(TypeError, msg):
com._mut_exclusive(a=1, b=2)
assert com._mut_exclusive(a=1, b=None) == 1
assert com._mut_exclusive(major=None, major_axis=None) is None
def test_is_sequence():
is_seq = com.is_sequence
assert (is_seq((1, 2)))
assert (is_seq([1, 2]))
assert (not is_seq("abcd"))
assert (not is_seq(u("abcd")))
assert (not is_seq(np.int64))
class A(object):
def __getitem__(self):
return 1
assert (not is_seq(A()))
def test_get_callable_name():
from functools import partial
getname = com._get_callable_name
def fn(x):
return x
lambda_ = lambda x: x
part1 = partial(fn)
part2 = partial(part1)
class somecall(object):
def __call__(self):
return x # noqa
assert getname(fn) == 'fn'
assert getname(lambda_)
assert getname(part1) == 'fn'
assert getname(part2) == 'fn'
assert getname(somecall()) == 'somecall'
assert getname(1) is None
class TestInferDtype(tm.TestCase):
def test_infer_dtype_from_scalar(self):
# Test that _infer_dtype_from_scalar is returning correct dtype for int
# and float.
for dtypec in [np.uint8, np.int8, np.uint16, np.int16, np.uint32,
np.int32, np.uint64, np.int64]:
data = dtypec(12)
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, type(data))
data = 12
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, np.int64)
for dtypec in [np.float16, np.float32, np.float64]:
data = dtypec(12)
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, dtypec)
data = np.float(12)
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, np.float64)
for data in [True, False]:
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, np.bool_)
for data in [np.complex64(1), np.complex128(1)]:
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, np.complex_)
import datetime
for data in [np.datetime64(1, 'ns'), pd.Timestamp(1),
datetime.datetime(2000, 1, 1, 0, 0)]:
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, 'M8[ns]')
for data in [np.timedelta64(1, 'ns'), pd.Timedelta(1),
datetime.timedelta(1)]:
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, 'm8[ns]')
for data in [datetime.date(2000, 1, 1),
pd.Timestamp(1, tz='US/Eastern'), 'foo']:
dtype, val = com._infer_dtype_from_scalar(data)
self.assertEqual(dtype, np.object_)
def test_notnull():
assert notnull(1.)
assert not notnull(None)
assert not notnull(np.NaN)
with cf.option_context("mode.use_inf_as_null", False):
assert notnull(np.inf)
assert notnull(-np.inf)
arr = np.array([1.5, np.inf, 3.5, -np.inf])
result = notnull(arr)
assert result.all()
with cf.option_context("mode.use_inf_as_null", True):
assert not notnull(np.inf)
assert not notnull(-np.inf)
arr = np.array([1.5, np.inf, 3.5, -np.inf])
result = notnull(arr)
assert result.sum() == 2
with cf.option_context("mode.use_inf_as_null", False):
for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
tm.makeObjectSeries(), tm.makeTimeSeries(),
tm.makePeriodSeries()]:
assert (isinstance(isnull(s), Series))
def test_isnull():
assert not isnull(1.)
assert isnull(None)
assert isnull(np.NaN)
assert not isnull(np.inf)
assert not isnull(-np.inf)
# series
for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
tm.makeObjectSeries(), tm.makeTimeSeries(),
tm.makePeriodSeries()]:
assert (isinstance(isnull(s), Series))
# frame
for df in [tm.makeTimeDataFrame(), tm.makePeriodFrame(),
tm.makeMixedDataFrame()]:
result = isnull(df)
expected = df.apply(isnull)
tm.assert_frame_equal(result, expected)
# panel
for p in [tm.makePanel(), tm.makePeriodPanel(), tm.add_nans(tm.makePanel())
]:
result = isnull(p)
expected = p.apply(isnull)
tm.assert_panel_equal(result, expected)
# panel 4d
for p in [tm.makePanel4D(), tm.add_nans_panel4d(tm.makePanel4D())]:
result = isnull(p)
expected = p.apply(isnull)
tm.assert_panel4d_equal(result, expected)
def test_isnull_lists():
result = isnull([[False]])
exp = np.array([[False]])
assert (np.array_equal(result, exp))
result = isnull([[1], [2]])
exp = np.array([[False], [False]])
assert (np.array_equal(result, exp))
# list of strings / unicode
result = isnull(['foo', 'bar'])
assert (not result.any())
result = isnull([u('foo'), u('bar')])
assert (not result.any())
def test_isnull_nat():
result = isnull([NaT])
exp = np.array([True])
assert (np.array_equal(result, exp))
result = isnull(np.array([NaT], dtype=object))
exp = np.array([True])
assert (np.array_equal(result, exp))
def test_isnull_numpy_nat():
arr = np.array([NaT, np.datetime64('NaT'), np.timedelta64('NaT'),
np.datetime64('NaT', 's')])
result = isnull(arr)
expected = np.array([True] * 4)
tm.assert_numpy_array_equal(result, expected)
def test_isnull_datetime():
assert (not isnull(datetime.now()))
assert notnull(datetime.now())
idx = date_range('1/1/1990', periods=20)
assert (notnull(idx).all())
idx = np.asarray(idx)
idx[0] = iNaT
idx = DatetimeIndex(idx)
mask = isnull(idx)
assert (mask[0])
assert (not mask[1:].any())
# GH 9129
pidx = idx.to_period(freq='M')
mask = isnull(pidx)
assert (mask[0])
assert (not mask[1:].any())
mask = isnull(pidx[1:])
assert (not mask.any())
class TestIsNull(tm.TestCase):
def test_0d_array(self):
self.assertTrue(isnull(np.array(np.nan)))
self.assertFalse(isnull(np.array(0.0)))
self.assertFalse(isnull(np.array(0)))
# test object dtype
self.assertTrue(isnull(np.array(np.nan, dtype=object)))
self.assertFalse(isnull(np.array(0.0, dtype=object)))
self.assertFalse(isnull(np.array(0, dtype=object)))
class TestNumberScalar(tm.TestCase):
def test_is_number(self):
self.assertTrue(com.is_number(True))
self.assertTrue(com.is_number(1))
self.assertTrue(com.is_number(1.1))
self.assertTrue(com.is_number(1 + 3j))
self.assertTrue(com.is_number(np.bool(False)))
self.assertTrue(com.is_number(np.int64(1)))
self.assertTrue(com.is_number(np.float64(1.1)))
self.assertTrue(com.is_number(np.complex128(1 + 3j)))
self.assertTrue(com.is_number(np.nan))
self.assertFalse(com.is_number(None))
self.assertFalse(com.is_number('x'))
self.assertFalse(com.is_number(datetime(2011, 1, 1)))
self.assertFalse(com.is_number(np.datetime64('2011-01-01')))
self.assertFalse(com.is_number(pd.Timestamp('2011-01-01')))
self.assertFalse(com.is_number(pd.Timestamp('2011-01-01',
tz='US/Eastern')))
self.assertFalse(com.is_number(timedelta(1000)))
self.assertFalse(com.is_number(pd.Timedelta('1 days')))
# questionable
self.assertFalse(com.is_number(np.bool_(False)))
self.assertTrue(com.is_number(np.timedelta64(1, 'D')))
def test_is_bool(self):
self.assertTrue(com.is_bool(True))
self.assertTrue(com.is_bool(np.bool(False)))
self.assertTrue(com.is_bool(np.bool_(False)))
self.assertFalse(com.is_bool(1))
self.assertFalse(com.is_bool(1.1))
self.assertFalse(com.is_bool(1 + 3j))
self.assertFalse(com.is_bool(np.int64(1)))
self.assertFalse(com.is_bool(np.float64(1.1)))
self.assertFalse(com.is_bool(np.complex128(1 + 3j)))
self.assertFalse(com.is_bool(np.nan))
self.assertFalse(com.is_bool(None))
self.assertFalse(com.is_bool('x'))
self.assertFalse(com.is_bool(datetime(2011, 1, 1)))
self.assertFalse(com.is_bool(np.datetime64('2011-01-01')))
self.assertFalse(com.is_bool(pd.Timestamp('2011-01-01')))
self.assertFalse(com.is_bool(pd.Timestamp('2011-01-01',
tz='US/Eastern')))
self.assertFalse(com.is_bool(timedelta(1000)))
self.assertFalse(com.is_bool(np.timedelta64(1, 'D')))
self.assertFalse(com.is_bool(pd.Timedelta('1 days')))
def test_is_integer(self):
self.assertTrue(com.is_integer(1))
self.assertTrue(com.is_integer(np.int64(1)))
self.assertFalse(com.is_integer(True))
self.assertFalse(com.is_integer(1.1))
self.assertFalse(com.is_integer(1 + 3j))
self.assertFalse(com.is_integer(np.bool(False)))
self.assertFalse(com.is_integer(np.bool_(False)))
self.assertFalse(com.is_integer(np.float64(1.1)))
self.assertFalse(com.is_integer(np.complex128(1 + 3j)))
self.assertFalse(com.is_integer(np.nan))
self.assertFalse(com.is_integer(None))
self.assertFalse(com.is_integer('x'))
self.assertFalse(com.is_integer(datetime(2011, 1, 1)))
self.assertFalse(com.is_integer(np.datetime64('2011-01-01')))
self.assertFalse(com.is_integer(pd.Timestamp('2011-01-01')))
self.assertFalse(com.is_integer(pd.Timestamp('2011-01-01',
tz='US/Eastern')))
self.assertFalse(com.is_integer(timedelta(1000)))
self.assertFalse(com.is_integer(pd.Timedelta('1 days')))
# questionable
self.assertTrue(com.is_integer(np.timedelta64(1, 'D')))
def test_is_float(self):
self.assertTrue(com.is_float(1.1))
self.assertTrue(com.is_float(np.float64(1.1)))
self.assertTrue(com.is_float(np.nan))
self.assertFalse(com.is_float(True))
self.assertFalse(com.is_float(1))
self.assertFalse(com.is_float(1 + 3j))
self.assertFalse(com.is_float(np.bool(False)))
self.assertFalse(com.is_float(np.bool_(False)))
self.assertFalse(com.is_float(np.int64(1)))
self.assertFalse(com.is_float(np.complex128(1 + 3j)))
self.assertFalse(com.is_float(None))
self.assertFalse(com.is_float('x'))
self.assertFalse(com.is_float(datetime(2011, 1, 1)))
self.assertFalse(com.is_float(np.datetime64('2011-01-01')))
self.assertFalse(com.is_float(pd.Timestamp('2011-01-01')))
self.assertFalse(com.is_float(pd.Timestamp('2011-01-01',
tz='US/Eastern')))
self.assertFalse(com.is_float(timedelta(1000)))
self.assertFalse(com.is_float(np.timedelta64(1, 'D')))
self.assertFalse(com.is_float(pd.Timedelta('1 days')))
def test_downcast_conv():
# test downcasting
arr = np.array([8.5, 8.6, 8.7, 8.8, 8.9999999999995])
result = com._possibly_downcast_to_dtype(arr, 'infer')
assert (np.array_equal(result, arr))
arr = np.array([8., 8., 8., 8., 8.9999999999995])
result = com._possibly_downcast_to_dtype(arr, 'infer')
expected = np.array([8, 8, 8, 8, 9])
assert (np.array_equal(result, expected))
arr = np.array([8., 8., 8., 8., 9.0000000000005])
result = com._possibly_downcast_to_dtype(arr, 'infer')
expected = np.array([8, 8, 8, 8, 9])
assert (np.array_equal(result, expected))
# conversions
expected = np.array([1, 2])
for dtype in [np.float64, object, np.int64]:
arr = np.array([1.0, 2.0], dtype=dtype)
result = com._possibly_downcast_to_dtype(arr, 'infer')
tm.assert_almost_equal(result, expected, check_dtype=False)
for dtype in [np.float64, object]:
expected = np.array([1.0, 2.0, np.nan], dtype=dtype)
arr = np.array([1.0, 2.0, np.nan], dtype=dtype)
result = com._possibly_downcast_to_dtype(arr, 'infer')
tm.assert_almost_equal(result, expected)
# empties
for dtype in [np.int32, np.float64, np.float32, np.bool_,
np.int64, object]:
arr = np.array([], dtype=dtype)
result = com._possibly_downcast_to_dtype(arr, 'int64')
tm.assert_almost_equal(result, np.array([], dtype=np.int64))
assert result.dtype == np.int64
def test_array_equivalent():
assert array_equivalent(np.array([np.nan, np.nan]),
np.array([np.nan, np.nan]))
assert array_equivalent(np.array([np.nan, 1, np.nan]),
np.array([np.nan, 1, np.nan]))
assert array_equivalent(np.array([np.nan, None], dtype='object'),
np.array([np.nan, None], dtype='object'))
assert array_equivalent(np.array([np.nan, 1 + 1j], dtype='complex'),
np.array([np.nan, 1 + 1j], dtype='complex'))
assert not array_equivalent(
np.array([np.nan, 1 + 1j], dtype='complex'), np.array(
[np.nan, 1 + 2j], dtype='complex'))
assert not array_equivalent(
np.array([np.nan, 1, np.nan]), np.array([np.nan, 2, np.nan]))
assert not array_equivalent(
np.array(['a', 'b', 'c', 'd']), np.array(['e', 'e']))
assert array_equivalent(Float64Index([0, np.nan]),
Float64Index([0, np.nan]))
assert not array_equivalent(
Float64Index([0, np.nan]), Float64Index([1, np.nan]))
assert array_equivalent(DatetimeIndex([0, np.nan]),
DatetimeIndex([0, np.nan]))
assert not array_equivalent(
DatetimeIndex([0, np.nan]), DatetimeIndex([1, np.nan]))
assert array_equivalent(TimedeltaIndex([0, np.nan]),
TimedeltaIndex([0, np.nan]))
assert not array_equivalent(
TimedeltaIndex([0, np.nan]), TimedeltaIndex([1, np.nan]))
assert array_equivalent(DatetimeIndex([0, np.nan], tz='US/Eastern'),
DatetimeIndex([0, np.nan], tz='US/Eastern'))
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz='US/Eastern'), DatetimeIndex(
[1, np.nan], tz='US/Eastern'))
assert not array_equivalent(
DatetimeIndex([0, np.nan]), DatetimeIndex(
[0, np.nan], tz='US/Eastern'))
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz='CET'), DatetimeIndex(
[0, np.nan], tz='US/Eastern'))
assert not array_equivalent(
DatetimeIndex([0, np.nan]), TimedeltaIndex([0, np.nan]))
def test_array_equivalent_str():
for dtype in ['O', 'S', 'U']:
assert array_equivalent(np.array(['A', 'B'], dtype=dtype),
np.array(['A', 'B'], dtype=dtype))
assert not array_equivalent(np.array(['A', 'B'], dtype=dtype),
np.array(['A', 'X'], dtype=dtype))
def test_datetimeindex_from_empty_datetime64_array():
for unit in ['ms', 'us', 'ns']:
idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit))
assert (len(idx) == 0)
def test_nan_to_nat_conversions():
df = DataFrame(dict({
'A': np.asarray(
lrange(10), dtype='float64'),
'B': Timestamp('20010101')
}))
df.iloc[3:6, :] = np.nan
result = df.loc[4, 'B'].value
assert (result == iNaT)
s = df['B'].copy()
s._data = s._data.setitem(indexer=tuple([slice(8, 9)]), value=np.nan)
assert (isnull(s[8]))
# numpy < 1.7.0 is wrong
from distutils.version import LooseVersion
if LooseVersion(np.__version__) >= '1.7.0':
assert (s[8].value == np.datetime64('NaT').astype(np.int64))
def test_any_none():
assert (com._any_none(1, 2, 3, None))
assert (not com._any_none(1, 2, 3, 4))
def test_all_not_none():
assert (com._all_not_none(1, 2, 3, 4))
assert (not com._all_not_none(1, 2, 3, None))
assert (not com._all_not_none(None, None, None, None))
def test_iterpairs():
data = [1, 2, 3, 4]
expected = [(1, 2), (2, 3), (3, 4)]
result = list(com.iterpairs(data))
assert (result == expected)
def test_split_ranges():
def _bin(x, width):
"return int(x) as a base2 string of given width"
return ''.join(str((x >> i) & 1) for i in range(width - 1, -1, -1))
def test_locs(mask):
nfalse = sum(np.array(mask) == 0)
remaining = 0
for s, e in com.split_ranges(mask):
remaining += e - s
assert 0 not in mask[s:e]
# make sure the total items covered by the ranges are a complete cover
assert remaining + nfalse == len(mask)
# exhaustively test all possible mask sequences of length 8
ncols = 8
for i in range(2 ** ncols):
cols = lmap(int, list(_bin(i, ncols))) # count up in base2
mask = [cols[i] == 1 for i in range(len(cols))]
test_locs(mask)
# base cases
test_locs([])
test_locs([0])
test_locs([1])
def test_map_indices_py():
data = [4, 3, 2, 1]
expected = {4: 0, 3: 1, 2: 2, 1: 3}
result = com.map_indices_py(data)
assert (result == expected)
def test_union():
a = [1, 2, 3]
b = [4, 5, 6]
union = sorted(com.union(a, b))
assert ((a + b) == union)
def test_difference():
a = [1, 2, 3]
b = [1, 2, 3, 4, 5, 6]
inter = sorted(com.difference(b, a))
assert ([4, 5, 6] == inter)
def test_intersection():
a = [1, 2, 3]
b = [1, 2, 3, 4, 5, 6]
inter = sorted(com.intersection(a, b))
assert (a == inter)
def test_groupby():
values = ['foo', 'bar', 'baz', 'baz2', 'qux', 'foo3']
expected = {'f': ['foo', 'foo3'],
'b': ['bar', 'baz', 'baz2'],
'q': ['qux']}
grouped = com.groupby(values, lambda x: x[0])
for k, v in grouped:
assert v == expected[k]
def test_is_list_like():
passes = ([], [1], (1, ), (1, 2), {'a': 1}, set([1, 'a']), Series([1]),
Series([]), Series(['a']).str)
fails = (1, '2', object())
for p in passes:
assert com.is_list_like(p)
for f in fails:
assert not com.is_list_like(f)
def test_is_dict_like():
passes = [{}, {'A': 1}, pd.Series([1])]
fails = ['1', 1, [1, 2], (1, 2), range(2), pd.Index([1])]
for p in passes:
assert com.is_dict_like(p)
for f in fails:
assert not com.is_dict_like(f)
def test_is_named_tuple():
passes = (collections.namedtuple('Test', list('abc'))(1, 2, 3), )
fails = ((1, 2, 3), 'a', Series({'pi': 3.14}))
for p in passes:
assert com.is_named_tuple(p)
for f in fails:
assert not com.is_named_tuple(f)
def test_is_hashable():
# all new-style classes are hashable by default
class HashableClass(object):
pass
class UnhashableClass1(object):
__hash__ = None
class UnhashableClass2(object):
def __hash__(self):
raise TypeError("Not hashable")
hashable = (1,
3.14,
np.float64(3.14),
'a',
tuple(),
(1, ),
HashableClass(), )
not_hashable = ([], UnhashableClass1(), )
abc_hashable_not_really_hashable = (([], ), UnhashableClass2(), )
for i in hashable:
assert com.is_hashable(i)
for i in not_hashable:
assert not com.is_hashable(i)
for i in abc_hashable_not_really_hashable:
assert not com.is_hashable(i)
# numpy.array is no longer collections.Hashable as of
# https://github.com/numpy/numpy/pull/5326, just test
# pandas.common.is_hashable()
assert not com.is_hashable(np.array([]))
# old-style classes in Python 2 don't appear hashable to
# collections.Hashable but also seem to support hash() by default
if compat.PY2:
class OldStyleClass():
pass
c = OldStyleClass()
assert not isinstance(c, collections.Hashable)
assert com.is_hashable(c)
hash(c) # this will not raise
def test_ensure_int32():
values = np.arange(10, dtype=np.int32)
result = com._ensure_int32(values)
assert (result.dtype == np.int32)
values = np.arange(10, dtype=np.int64)
result = com._ensure_int32(values)
assert (result.dtype == np.int32)
def test_is_re():
passes = re.compile('ad'),
fails = 'x', 2, 3, object()
for p in passes:
assert com.is_re(p)
for f in fails:
assert not com.is_re(f)
def test_is_recompilable():
passes = (r'a', u('x'), r'asdf', re.compile('adsf'), u(r'\u2233\s*'),
re.compile(r''))
fails = 1, [], object()
for p in passes:
assert com.is_re_compilable(p)
for f in fails:
assert not com.is_re_compilable(f)
def test_random_state():
import numpy.random as npr
# Check with seed
state = com._random_state(5)
tm.assert_equal(state.uniform(), npr.RandomState(5).uniform())
# Check with random state object
state2 = npr.RandomState(10)
tm.assert_equal(
com._random_state(state2).uniform(), npr.RandomState(10).uniform())
# check with no arg random state
assert com._random_state() is np.random
# Error for floats or strings
with tm.assertRaises(ValueError):
com._random_state('test')
with tm.assertRaises(ValueError):
com._random_state(5.5)
def test_maybe_match_name():
matched = com._maybe_match_name(
Series([1], name='x'), Series(
[2], name='x'))
assert (matched == 'x')
matched = com._maybe_match_name(
Series([1], name='x'), Series(
[2], name='y'))
assert (matched is None)
matched = com._maybe_match_name(Series([1]), Series([2], name='x'))
assert (matched is None)
matched = com._maybe_match_name(Series([1], name='x'), Series([2]))
assert (matched is None)
matched = com._maybe_match_name(Series([1], name='x'), [2])
assert (matched == 'x')
matched = com._maybe_match_name([1], Series([2], name='y'))
assert (matched == 'y')
class TestMaybe(tm.TestCase):
def test_maybe_convert_string_to_array(self):
result = com._maybe_convert_string_to_object('x')
tm.assert_numpy_array_equal(result, np.array(['x'], dtype=object))
self.assertTrue(result.dtype == object)
result = com._maybe_convert_string_to_object(1)
self.assertEqual(result, 1)
arr = np.array(['x', 'y'], dtype=str)
result = com._maybe_convert_string_to_object(arr)
tm.assert_numpy_array_equal(result, np.array(['x', 'y'], dtype=object))
self.assertTrue(result.dtype == object)
# unicode
arr = np.array(['x', 'y']).astype('U')
result = com._maybe_convert_string_to_object(arr)
tm.assert_numpy_array_equal(result, np.array(['x', 'y'], dtype=object))
self.assertTrue(result.dtype == object)
# object
arr = np.array(['x', 2], dtype=object)
result = com._maybe_convert_string_to_object(arr)
tm.assert_numpy_array_equal(result, np.array(['x', 2], dtype=object))
self.assertTrue(result.dtype == object)
def test_maybe_convert_scalar(self):
# pass thru
result = com._maybe_convert_scalar('x')
self.assertEqual(result, 'x')
result = com._maybe_convert_scalar(np.array([1]))
self.assertEqual(result, np.array([1]))
# leave scalar dtype
result = com._maybe_convert_scalar(np.int64(1))
self.assertEqual(result, np.int64(1))
result = com._maybe_convert_scalar(np.int32(1))
self.assertEqual(result, np.int32(1))
result = com._maybe_convert_scalar(np.float32(1))
self.assertEqual(result, np.float32(1))
result = com._maybe_convert_scalar(np.int64(1))
self.assertEqual(result, np.float64(1))
# coerce
result = com._maybe_convert_scalar(1)
self.assertEqual(result, np.int64(1))
result = com._maybe_convert_scalar(1.0)
self.assertEqual(result, np.float64(1))
result = com._maybe_convert_scalar(pd.Timestamp('20130101'))
self.assertEqual(result, pd.Timestamp('20130101').value)
result = com._maybe_convert_scalar(datetime(2013, 1, 1))
self.assertEqual(result, pd.Timestamp('20130101').value)
result = com._maybe_convert_scalar(pd.Timedelta('1 day 1 min'))
self.assertEqual(result, pd.Timedelta('1 day 1 min').value)
class TestConvert(tm.TestCase):
def test_possibly_convert_objects_copy(self):
values = np.array([1, 2])
out = convert._possibly_convert_objects(values, copy=False)
self.assertTrue(values is out)
out = convert._possibly_convert_objects(values, copy=True)
self.assertTrue(values is not out)
values = np.array(['apply', 'banana'])
out = convert._possibly_convert_objects(values, copy=False)
self.assertTrue(values is out)
out = convert._possibly_convert_objects(values, copy=True)
self.assertTrue(values is not out)
def test_dict_compat():
data_datetime64 = {np.datetime64('1990-03-15'): 1,
np.datetime64('2015-03-15'): 2}
data_unchanged = {1: 2, 3: 4, 5: 6}
expected = {Timestamp('1990-3-15'): 1, Timestamp('2015-03-15'): 2}
assert (com._dict_compat(data_datetime64) == expected)
assert (com._dict_compat(expected) == expected)
assert (com._dict_compat(data_unchanged) == data_unchanged)
def test_is_timedelta():
assert (com.is_timedelta64_dtype('timedelta64'))
assert (com.is_timedelta64_dtype('timedelta64[ns]'))
assert (not com.is_timedelta64_ns_dtype('timedelta64'))
assert (com.is_timedelta64_ns_dtype('timedelta64[ns]'))
tdi = TimedeltaIndex([1e14, 2e14], dtype='timedelta64')
assert (com.is_timedelta64_dtype(tdi))
assert (com.is_timedelta64_ns_dtype(tdi))
assert (com.is_timedelta64_ns_dtype(tdi.astype('timedelta64[ns]')))
# Conversion to Int64Index:
assert (not com.is_timedelta64_ns_dtype(tdi.astype('timedelta64')))
assert (not com.is_timedelta64_ns_dtype(tdi.astype('timedelta64[h]')))
def test_array_equivalent_compat():
# see gh-13388
m = np.array([(1, 2), (3, 4)], dtype=[('a', int), ('b', float)])
n = np.array([(1, 2), (3, 4)], dtype=[('a', int), ('b', float)])
assert (com.array_equivalent(m, n, strict_nan=True))
assert (com.array_equivalent(m, n, strict_nan=False))
m = np.array([(1, 2), (3, 4)], dtype=[('a', int), ('b', float)])
n = np.array([(1, 2), (4, 3)], dtype=[('a', int), ('b', float)])
assert (not com.array_equivalent(m, n, strict_nan=True))
assert (not com.array_equivalent(m, n, strict_nan=False))
m = np.array([(1, 2), (3, 4)], dtype=[('a', int), ('b', float)])
n = np.array([(1, 2), (3, 4)], dtype=[('b', int), ('a', float)])
assert (not com.array_equivalent(m, n, strict_nan=True))
assert (not com.array_equivalent(m, n, strict_nan=False))
if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)