MCPcopy
hub / github.com/pandas-dev/pandas / test_convert

Method test_convert

pandas/tests/internals/test_internals.py:590–647  ·  view source on GitHub ↗
(self, using_infer_string)

Source from the content-addressed store, hash-verified

588 assert tmgr.iget(3).dtype.type == t
589
590 def test_convert(self, using_infer_string):
591 def _compare(old_mgr, new_mgr):
592 """compare the blocks, numeric compare ==, object don't"""
593 old_blocks = set(old_mgr.blocks)
594 new_blocks = set(new_mgr.blocks)
595 assert len(old_blocks) == len(new_blocks)
596
597 # compare non-numeric
598 for b in old_blocks:
599 found = False
600 for nb in new_blocks:
601 if (b.values == nb.values).all():
602 found = True
603 break
604 assert found
605
606 for b in new_blocks:
607 found = False
608 for ob in old_blocks:
609 if (b.values == ob.values).all():
610 found = True
611 break
612 assert found
613
614 # noops
615 mgr = create_mgr("f: i8; g: f8")
616 new_mgr = mgr.convert()
617 _compare(mgr, new_mgr)
618
619 # convert
620 mgr = create_mgr("a,b,foo: object; f: i8; g: f8")
621 mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
622 mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
623 mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
624 new_mgr = mgr.convert()
625 dtype = "str" if using_infer_string else np.object_
626 assert new_mgr.iget(0).dtype == dtype
627 assert new_mgr.iget(1).dtype == dtype
628 assert new_mgr.iget(2).dtype == dtype
629 assert new_mgr.iget(3).dtype == np.int64
630 assert new_mgr.iget(4).dtype == np.float64
631
632 mgr = create_mgr(
633 "a,b,foo: object; f: i4; bool: bool; dt: datetime; i: i8; g: f8; h: f2"
634 )
635 mgr.iset(0, np.array(["1"] * N, dtype=np.object_))
636 mgr.iset(1, np.array(["2."] * N, dtype=np.object_))
637 mgr.iset(2, np.array(["foo."] * N, dtype=np.object_))
638 new_mgr = mgr.convert()
639 assert new_mgr.iget(0).dtype == dtype
640 assert new_mgr.iget(1).dtype == dtype
641 assert new_mgr.iget(2).dtype == dtype
642 assert new_mgr.iget(3).dtype == np.int32
643 assert new_mgr.iget(4).dtype == np.bool_
644 assert new_mgr.iget(5).dtype.type, np.datetime64
645 assert new_mgr.iget(6).dtype == np.int64
646 assert new_mgr.iget(7).dtype == np.float64
647 assert new_mgr.iget(8).dtype == np.float16

Callers

nothing calls this directly

Calls 5

create_mgrFunction · 0.85
isetMethod · 0.80
convertMethod · 0.45
arrayMethod · 0.45
igetMethod · 0.45

Tested by

no test coverage detected