test_invalid.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from io import StringIO
  2. import numpy as np
  3. import pytest
  4. from pandas import (
  5. DataFrame,
  6. concat,
  7. read_csv,
  8. )
  9. import pandas._testing as tm
  10. class TestInvalidConcat:
  11. def test_concat_invalid(self):
  12. # trying to concat a ndframe with a non-ndframe
  13. df1 = tm.makeCustomDataframe(10, 2)
  14. for obj in [1, {}, [1, 2], (1, 2)]:
  15. msg = (
  16. f"cannot concatenate object of type '{type(obj)}'; "
  17. "only Series and DataFrame objs are valid"
  18. )
  19. with pytest.raises(TypeError, match=msg):
  20. concat([df1, obj])
  21. def test_concat_invalid_first_argument(self):
  22. df1 = tm.makeCustomDataframe(10, 2)
  23. msg = (
  24. "first argument must be an iterable of pandas "
  25. 'objects, you passed an object of type "DataFrame"'
  26. )
  27. with pytest.raises(TypeError, match=msg):
  28. concat(df1)
  29. def test_concat_generator_obj(self):
  30. # generator ok though
  31. concat(DataFrame(np.random.rand(5, 5)) for _ in range(3))
  32. def test_concat_textreader_obj(self):
  33. # text reader ok
  34. # GH6583
  35. data = """index,A,B,C,D
  36. foo,2,3,4,5
  37. bar,7,8,9,10
  38. baz,12,13,14,15
  39. qux,12,13,14,15
  40. foo2,12,13,14,15
  41. bar2,12,13,14,15
  42. """
  43. with read_csv(StringIO(data), chunksize=1) as reader:
  44. result = concat(reader, ignore_index=True)
  45. expected = read_csv(StringIO(data))
  46. tm.assert_frame_equal(result, expected)