test_constructors.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. # coding=utf-8
  2. # pylint: disable-msg=E1101,W0612
  3. from collections import OrderedDict
  4. from datetime import datetime, timedelta
  5. import numpy as np
  6. from numpy import nan
  7. import numpy.ma as ma
  8. import pytest
  9. from pandas._libs import lib
  10. from pandas._libs.tslib import iNaT
  11. from pandas.compat import PY36, long, lrange, range, zip
  12. from pandas.core.dtypes.common import (
  13. is_categorical_dtype, is_datetime64tz_dtype)
  14. import pandas as pd
  15. from pandas import (
  16. Categorical, DataFrame, Index, IntervalIndex, MultiIndex, NaT, Series,
  17. Timestamp, date_range, isna, period_range, timedelta_range)
  18. from pandas.api.types import CategoricalDtype
  19. from pandas.core.arrays import period_array
  20. import pandas.util.testing as tm
  21. from pandas.util.testing import assert_series_equal
  22. class TestSeriesConstructors():
  23. def test_invalid_dtype(self):
  24. # GH15520
  25. msg = 'not understood'
  26. invalid_list = [pd.Timestamp, 'pd.Timestamp', list]
  27. for dtype in invalid_list:
  28. with pytest.raises(TypeError, match=msg):
  29. Series([], name='time', dtype=dtype)
  30. def test_scalar_conversion(self):
  31. # Pass in scalar is disabled
  32. scalar = Series(0.5)
  33. assert not isinstance(scalar, float)
  34. # Coercion
  35. assert float(Series([1.])) == 1.0
  36. assert int(Series([1.])) == 1
  37. assert long(Series([1.])) == 1
  38. def test_constructor(self, datetime_series, empty_series):
  39. assert datetime_series.index.is_all_dates
  40. # Pass in Series
  41. derived = Series(datetime_series)
  42. assert derived.index.is_all_dates
  43. assert tm.equalContents(derived.index, datetime_series.index)
  44. # Ensure new index is not created
  45. assert id(datetime_series.index) == id(derived.index)
  46. # Mixed type Series
  47. mixed = Series(['hello', np.NaN], index=[0, 1])
  48. assert mixed.dtype == np.object_
  49. assert mixed[1] is np.NaN
  50. assert not empty_series.index.is_all_dates
  51. assert not Series({}).index.is_all_dates
  52. # exception raised is of type Exception
  53. with pytest.raises(Exception, match="Data must be 1-dimensional"):
  54. Series(np.random.randn(3, 3), index=np.arange(3))
  55. mixed.name = 'Series'
  56. rs = Series(mixed).name
  57. xp = 'Series'
  58. assert rs == xp
  59. # raise on MultiIndex GH4187
  60. m = MultiIndex.from_arrays([[1, 2], [3, 4]])
  61. msg = "initializing a Series from a MultiIndex is not supported"
  62. with pytest.raises(NotImplementedError, match=msg):
  63. Series(m)
  64. @pytest.mark.parametrize('input_class', [list, dict, OrderedDict])
  65. def test_constructor_empty(self, input_class):
  66. empty = Series()
  67. empty2 = Series(input_class())
  68. # these are Index() and RangeIndex() which don't compare type equal
  69. # but are just .equals
  70. assert_series_equal(empty, empty2, check_index_type=False)
  71. # With explicit dtype:
  72. empty = Series(dtype='float64')
  73. empty2 = Series(input_class(), dtype='float64')
  74. assert_series_equal(empty, empty2, check_index_type=False)
  75. # GH 18515 : with dtype=category:
  76. empty = Series(dtype='category')
  77. empty2 = Series(input_class(), dtype='category')
  78. assert_series_equal(empty, empty2, check_index_type=False)
  79. if input_class is not list:
  80. # With index:
  81. empty = Series(index=lrange(10))
  82. empty2 = Series(input_class(), index=lrange(10))
  83. assert_series_equal(empty, empty2)
  84. # With index and dtype float64:
  85. empty = Series(np.nan, index=lrange(10))
  86. empty2 = Series(input_class(), index=lrange(10), dtype='float64')
  87. assert_series_equal(empty, empty2)
  88. # GH 19853 : with empty string, index and dtype str
  89. empty = Series('', dtype=str, index=range(3))
  90. empty2 = Series('', index=range(3))
  91. assert_series_equal(empty, empty2)
  92. @pytest.mark.parametrize('input_arg', [np.nan, float('nan')])
  93. def test_constructor_nan(self, input_arg):
  94. empty = Series(dtype='float64', index=lrange(10))
  95. empty2 = Series(input_arg, index=lrange(10))
  96. assert_series_equal(empty, empty2, check_index_type=False)
  97. @pytest.mark.parametrize('dtype', [
  98. 'f8', 'i8', 'M8[ns]', 'm8[ns]', 'category', 'object',
  99. 'datetime64[ns, UTC]',
  100. ])
  101. @pytest.mark.parametrize('index', [None, pd.Index([])])
  102. def test_constructor_dtype_only(self, dtype, index):
  103. # GH-20865
  104. result = pd.Series(dtype=dtype, index=index)
  105. assert result.dtype == dtype
  106. assert len(result) == 0
  107. def test_constructor_no_data_index_order(self):
  108. result = pd.Series(index=['b', 'a', 'c'])
  109. assert result.index.tolist() == ['b', 'a', 'c']
  110. def test_constructor_no_data_string_type(self):
  111. # GH 22477
  112. result = pd.Series(index=[1], dtype=str)
  113. assert np.isnan(result.iloc[0])
  114. @pytest.mark.parametrize('item', ['entry', 'ѐ', 13])
  115. def test_constructor_string_element_string_type(self, item):
  116. # GH 22477
  117. result = pd.Series(item, index=[1], dtype=str)
  118. assert result.iloc[0] == str(item)
  119. def test_constructor_dtype_str_na_values(self, string_dtype):
  120. # https://github.com/pandas-dev/pandas/issues/21083
  121. ser = Series(['x', None], dtype=string_dtype)
  122. result = ser.isna()
  123. expected = Series([False, True])
  124. tm.assert_series_equal(result, expected)
  125. assert ser.iloc[1] is None
  126. ser = Series(['x', np.nan], dtype=string_dtype)
  127. assert np.isnan(ser.iloc[1])
  128. def test_constructor_series(self):
  129. index1 = ['d', 'b', 'a', 'c']
  130. index2 = sorted(index1)
  131. s1 = Series([4, 7, -5, 3], index=index1)
  132. s2 = Series(s1, index=index2)
  133. assert_series_equal(s2, s1.sort_index())
  134. def test_constructor_iterable(self):
  135. # GH 21987
  136. class Iter():
  137. def __iter__(self):
  138. for i in range(10):
  139. yield i
  140. expected = Series(list(range(10)), dtype='int64')
  141. result = Series(Iter(), dtype='int64')
  142. assert_series_equal(result, expected)
  143. def test_constructor_sequence(self):
  144. # GH 21987
  145. expected = Series(list(range(10)), dtype='int64')
  146. result = Series(range(10), dtype='int64')
  147. assert_series_equal(result, expected)
  148. def test_constructor_single_str(self):
  149. # GH 21987
  150. expected = Series(['abc'])
  151. result = Series('abc')
  152. assert_series_equal(result, expected)
  153. def test_constructor_list_like(self):
  154. # make sure that we are coercing different
  155. # list-likes to standard dtypes and not
  156. # platform specific
  157. expected = Series([1, 2, 3], dtype='int64')
  158. for obj in [[1, 2, 3], (1, 2, 3),
  159. np.array([1, 2, 3], dtype='int64')]:
  160. result = Series(obj, index=[0, 1, 2])
  161. assert_series_equal(result, expected)
  162. @pytest.mark.parametrize('input_vals', [
  163. ([1, 2]),
  164. (['1', '2']),
  165. (list(pd.date_range('1/1/2011', periods=2, freq='H'))),
  166. (list(pd.date_range('1/1/2011', periods=2, freq='H',
  167. tz='US/Eastern'))),
  168. ([pd.Interval(left=0, right=5)]),
  169. ])
  170. def test_constructor_list_str(self, input_vals, string_dtype):
  171. # GH 16605
  172. # Ensure that data elements from a list are converted to strings
  173. # when dtype is str, 'str', or 'U'
  174. result = Series(input_vals, dtype=string_dtype)
  175. expected = Series(input_vals).astype(string_dtype)
  176. assert_series_equal(result, expected)
  177. def test_constructor_list_str_na(self, string_dtype):
  178. result = Series([1.0, 2.0, np.nan], dtype=string_dtype)
  179. expected = Series(['1.0', '2.0', np.nan], dtype=object)
  180. assert_series_equal(result, expected)
  181. assert np.isnan(result[2])
  182. def test_constructor_generator(self):
  183. gen = (i for i in range(10))
  184. result = Series(gen)
  185. exp = Series(lrange(10))
  186. assert_series_equal(result, exp)
  187. gen = (i for i in range(10))
  188. result = Series(gen, index=lrange(10, 20))
  189. exp.index = lrange(10, 20)
  190. assert_series_equal(result, exp)
  191. def test_constructor_map(self):
  192. # GH8909
  193. m = map(lambda x: x, range(10))
  194. result = Series(m)
  195. exp = Series(lrange(10))
  196. assert_series_equal(result, exp)
  197. m = map(lambda x: x, range(10))
  198. result = Series(m, index=lrange(10, 20))
  199. exp.index = lrange(10, 20)
  200. assert_series_equal(result, exp)
  201. def test_constructor_categorical(self):
  202. cat = pd.Categorical([0, 1, 2, 0, 1, 2], ['a', 'b', 'c'],
  203. fastpath=True)
  204. res = Series(cat)
  205. tm.assert_categorical_equal(res.values, cat)
  206. # can cast to a new dtype
  207. result = Series(pd.Categorical([1, 2, 3]),
  208. dtype='int64')
  209. expected = pd.Series([1, 2, 3], dtype='int64')
  210. tm.assert_series_equal(result, expected)
  211. # GH12574
  212. cat = Series(pd.Categorical([1, 2, 3]), dtype='category')
  213. assert is_categorical_dtype(cat)
  214. assert is_categorical_dtype(cat.dtype)
  215. s = Series([1, 2, 3], dtype='category')
  216. assert is_categorical_dtype(s)
  217. assert is_categorical_dtype(s.dtype)
  218. def test_constructor_categorical_with_coercion(self):
  219. factor = Categorical(['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c'])
  220. # test basic creation / coercion of categoricals
  221. s = Series(factor, name='A')
  222. assert s.dtype == 'category'
  223. assert len(s) == len(factor)
  224. str(s.values)
  225. str(s)
  226. # in a frame
  227. df = DataFrame({'A': factor})
  228. result = df['A']
  229. tm.assert_series_equal(result, s)
  230. result = df.iloc[:, 0]
  231. tm.assert_series_equal(result, s)
  232. assert len(df) == len(factor)
  233. str(df.values)
  234. str(df)
  235. df = DataFrame({'A': s})
  236. result = df['A']
  237. tm.assert_series_equal(result, s)
  238. assert len(df) == len(factor)
  239. str(df.values)
  240. str(df)
  241. # multiples
  242. df = DataFrame({'A': s, 'B': s, 'C': 1})
  243. result1 = df['A']
  244. result2 = df['B']
  245. tm.assert_series_equal(result1, s)
  246. tm.assert_series_equal(result2, s, check_names=False)
  247. assert result2.name == 'B'
  248. assert len(df) == len(factor)
  249. str(df.values)
  250. str(df)
  251. # GH8623
  252. x = DataFrame([[1, 'John P. Doe'], [2, 'Jane Dove'],
  253. [1, 'John P. Doe']],
  254. columns=['person_id', 'person_name'])
  255. x['person_name'] = Categorical(x.person_name
  256. ) # doing this breaks transform
  257. expected = x.iloc[0].person_name
  258. result = x.person_name.iloc[0]
  259. assert result == expected
  260. result = x.person_name[0]
  261. assert result == expected
  262. result = x.person_name.loc[0]
  263. assert result == expected
  264. def test_constructor_categorical_dtype(self):
  265. result = pd.Series(['a', 'b'],
  266. dtype=CategoricalDtype(['a', 'b', 'c'],
  267. ordered=True))
  268. assert is_categorical_dtype(result) is True
  269. tm.assert_index_equal(result.cat.categories, pd.Index(['a', 'b', 'c']))
  270. assert result.cat.ordered
  271. result = pd.Series(['a', 'b'], dtype=CategoricalDtype(['b', 'a']))
  272. assert is_categorical_dtype(result)
  273. tm.assert_index_equal(result.cat.categories, pd.Index(['b', 'a']))
  274. assert result.cat.ordered is False
  275. # GH 19565 - Check broadcasting of scalar with Categorical dtype
  276. result = Series('a', index=[0, 1],
  277. dtype=CategoricalDtype(['a', 'b'], ordered=True))
  278. expected = Series(['a', 'a'], index=[0, 1],
  279. dtype=CategoricalDtype(['a', 'b'], ordered=True))
  280. tm.assert_series_equal(result, expected, check_categorical=True)
  281. def test_categorical_sideeffects_free(self):
  282. # Passing a categorical to a Series and then changing values in either
  283. # the series or the categorical should not change the values in the
  284. # other one, IF you specify copy!
  285. cat = Categorical(["a", "b", "c", "a"])
  286. s = Series(cat, copy=True)
  287. assert s.cat is not cat
  288. s.cat.categories = [1, 2, 3]
  289. exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
  290. exp_cat = np.array(["a", "b", "c", "a"], dtype=np.object_)
  291. tm.assert_numpy_array_equal(s.__array__(), exp_s)
  292. tm.assert_numpy_array_equal(cat.__array__(), exp_cat)
  293. # setting
  294. s[0] = 2
  295. exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
  296. tm.assert_numpy_array_equal(s.__array__(), exp_s2)
  297. tm.assert_numpy_array_equal(cat.__array__(), exp_cat)
  298. # however, copy is False by default
  299. # so this WILL change values
  300. cat = Categorical(["a", "b", "c", "a"])
  301. s = Series(cat)
  302. assert s.values is cat
  303. s.cat.categories = [1, 2, 3]
  304. exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
  305. tm.assert_numpy_array_equal(s.__array__(), exp_s)
  306. tm.assert_numpy_array_equal(cat.__array__(), exp_s)
  307. s[0] = 2
  308. exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
  309. tm.assert_numpy_array_equal(s.__array__(), exp_s2)
  310. tm.assert_numpy_array_equal(cat.__array__(), exp_s2)
  311. def test_unordered_compare_equal(self):
  312. left = pd.Series(['a', 'b', 'c'],
  313. dtype=CategoricalDtype(['a', 'b']))
  314. right = pd.Series(pd.Categorical(['a', 'b', np.nan],
  315. categories=['a', 'b']))
  316. tm.assert_series_equal(left, right)
  317. def test_constructor_maskedarray(self):
  318. data = ma.masked_all((3, ), dtype=float)
  319. result = Series(data)
  320. expected = Series([nan, nan, nan])
  321. assert_series_equal(result, expected)
  322. data[0] = 0.0
  323. data[2] = 2.0
  324. index = ['a', 'b', 'c']
  325. result = Series(data, index=index)
  326. expected = Series([0.0, nan, 2.0], index=index)
  327. assert_series_equal(result, expected)
  328. data[1] = 1.0
  329. result = Series(data, index=index)
  330. expected = Series([0.0, 1.0, 2.0], index=index)
  331. assert_series_equal(result, expected)
  332. data = ma.masked_all((3, ), dtype=int)
  333. result = Series(data)
  334. expected = Series([nan, nan, nan], dtype=float)
  335. assert_series_equal(result, expected)
  336. data[0] = 0
  337. data[2] = 2
  338. index = ['a', 'b', 'c']
  339. result = Series(data, index=index)
  340. expected = Series([0, nan, 2], index=index, dtype=float)
  341. assert_series_equal(result, expected)
  342. data[1] = 1
  343. result = Series(data, index=index)
  344. expected = Series([0, 1, 2], index=index, dtype=int)
  345. assert_series_equal(result, expected)
  346. data = ma.masked_all((3, ), dtype=bool)
  347. result = Series(data)
  348. expected = Series([nan, nan, nan], dtype=object)
  349. assert_series_equal(result, expected)
  350. data[0] = True
  351. data[2] = False
  352. index = ['a', 'b', 'c']
  353. result = Series(data, index=index)
  354. expected = Series([True, nan, False], index=index, dtype=object)
  355. assert_series_equal(result, expected)
  356. data[1] = True
  357. result = Series(data, index=index)
  358. expected = Series([True, True, False], index=index, dtype=bool)
  359. assert_series_equal(result, expected)
  360. data = ma.masked_all((3, ), dtype='M8[ns]')
  361. result = Series(data)
  362. expected = Series([iNaT, iNaT, iNaT], dtype='M8[ns]')
  363. assert_series_equal(result, expected)
  364. data[0] = datetime(2001, 1, 1)
  365. data[2] = datetime(2001, 1, 3)
  366. index = ['a', 'b', 'c']
  367. result = Series(data, index=index)
  368. expected = Series([datetime(2001, 1, 1), iNaT,
  369. datetime(2001, 1, 3)], index=index, dtype='M8[ns]')
  370. assert_series_equal(result, expected)
  371. data[1] = datetime(2001, 1, 2)
  372. result = Series(data, index=index)
  373. expected = Series([datetime(2001, 1, 1), datetime(2001, 1, 2),
  374. datetime(2001, 1, 3)], index=index, dtype='M8[ns]')
  375. assert_series_equal(result, expected)
  376. def test_constructor_maskedarray_hardened(self):
  377. # Check numpy masked arrays with hard masks -- from GH24574
  378. data = ma.masked_all((3, ), dtype=float).harden_mask()
  379. result = pd.Series(data)
  380. expected = pd.Series([nan, nan, nan])
  381. tm.assert_series_equal(result, expected)
  382. def test_series_ctor_plus_datetimeindex(self):
  383. rng = date_range('20090415', '20090519', freq='B')
  384. data = {k: 1 for k in rng}
  385. result = Series(data, index=rng)
  386. assert result.index is rng
  387. def test_constructor_default_index(self):
  388. s = Series([0, 1, 2])
  389. tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
  390. @pytest.mark.parametrize('input', [[1, 2, 3],
  391. (1, 2, 3),
  392. list(range(3)),
  393. pd.Categorical(['a', 'b', 'a']),
  394. (i for i in range(3)),
  395. map(lambda x: x, range(3))])
  396. def test_constructor_index_mismatch(self, input):
  397. # GH 19342
  398. # test that construction of a Series with an index of different length
  399. # raises an error
  400. msg = 'Length of passed values is 3, index implies 4'
  401. with pytest.raises(ValueError, match=msg):
  402. Series(input, index=np.arange(4))
  403. def test_constructor_numpy_scalar(self):
  404. # GH 19342
  405. # construction with a numpy scalar
  406. # should not raise
  407. result = Series(np.array(100), index=np.arange(4), dtype='int64')
  408. expected = Series(100, index=np.arange(4), dtype='int64')
  409. tm.assert_series_equal(result, expected)
  410. def test_constructor_broadcast_list(self):
  411. # GH 19342
  412. # construction with single-element container and index
  413. # should raise
  414. msg = "Length of passed values is 1, index implies 3"
  415. with pytest.raises(ValueError, match=msg):
  416. Series(['foo'], index=['a', 'b', 'c'])
  417. def test_constructor_corner(self):
  418. df = tm.makeTimeDataFrame()
  419. objs = [df, df]
  420. s = Series(objs, index=[0, 1])
  421. assert isinstance(s, Series)
  422. def test_constructor_sanitize(self):
  423. s = Series(np.array([1., 1., 8.]), dtype='i8')
  424. assert s.dtype == np.dtype('i8')
  425. s = Series(np.array([1., 1., np.nan]), copy=True, dtype='i8')
  426. assert s.dtype == np.dtype('f8')
  427. def test_constructor_copy(self):
  428. # GH15125
  429. # test dtype parameter has no side effects on copy=True
  430. for data in [[1.], np.array([1.])]:
  431. x = Series(data)
  432. y = pd.Series(x, copy=True, dtype=float)
  433. # copy=True maintains original data in Series
  434. tm.assert_series_equal(x, y)
  435. # changes to origin of copy does not affect the copy
  436. x[0] = 2.
  437. assert not x.equals(y)
  438. assert x[0] == 2.
  439. assert y[0] == 1.
  440. @pytest.mark.parametrize(
  441. "index",
  442. [
  443. pd.date_range('20170101', periods=3, tz='US/Eastern'),
  444. pd.date_range('20170101', periods=3),
  445. pd.timedelta_range('1 day', periods=3),
  446. pd.period_range('2012Q1', periods=3, freq='Q'),
  447. pd.Index(list('abc')),
  448. pd.Int64Index([1, 2, 3]),
  449. pd.RangeIndex(0, 3)],
  450. ids=lambda x: type(x).__name__)
  451. def test_constructor_limit_copies(self, index):
  452. # GH 17449
  453. # limit copies of input
  454. s = pd.Series(index)
  455. # we make 1 copy; this is just a smoke test here
  456. assert s._data.blocks[0].values is not index
  457. def test_constructor_pass_none(self):
  458. s = Series(None, index=lrange(5))
  459. assert s.dtype == np.float64
  460. s = Series(None, index=lrange(5), dtype=object)
  461. assert s.dtype == np.object_
  462. # GH 7431
  463. # inference on the index
  464. s = Series(index=np.array([None]))
  465. expected = Series(index=Index([None]))
  466. assert_series_equal(s, expected)
  467. def test_constructor_pass_nan_nat(self):
  468. # GH 13467
  469. exp = Series([np.nan, np.nan], dtype=np.float64)
  470. assert exp.dtype == np.float64
  471. tm.assert_series_equal(Series([np.nan, np.nan]), exp)
  472. tm.assert_series_equal(Series(np.array([np.nan, np.nan])), exp)
  473. exp = Series([pd.NaT, pd.NaT])
  474. assert exp.dtype == 'datetime64[ns]'
  475. tm.assert_series_equal(Series([pd.NaT, pd.NaT]), exp)
  476. tm.assert_series_equal(Series(np.array([pd.NaT, pd.NaT])), exp)
  477. tm.assert_series_equal(Series([pd.NaT, np.nan]), exp)
  478. tm.assert_series_equal(Series(np.array([pd.NaT, np.nan])), exp)
  479. tm.assert_series_equal(Series([np.nan, pd.NaT]), exp)
  480. tm.assert_series_equal(Series(np.array([np.nan, pd.NaT])), exp)
  481. def test_constructor_cast(self):
  482. msg = "could not convert string to float"
  483. with pytest.raises(ValueError, match=msg):
  484. Series(["a", "b", "c"], dtype=float)
  485. def test_constructor_unsigned_dtype_overflow(self, uint_dtype):
  486. # see gh-15832
  487. msg = 'Trying to coerce negative values to unsigned integers'
  488. with pytest.raises(OverflowError, match=msg):
  489. Series([-1], dtype=uint_dtype)
  490. def test_constructor_coerce_float_fail(self, any_int_dtype):
  491. # see gh-15832
  492. msg = "Trying to coerce float values to integers"
  493. with pytest.raises(ValueError, match=msg):
  494. Series([1, 2, 3.5], dtype=any_int_dtype)
  495. def test_constructor_coerce_float_valid(self, float_dtype):
  496. s = Series([1, 2, 3.5], dtype=float_dtype)
  497. expected = Series([1, 2, 3.5]).astype(float_dtype)
  498. assert_series_equal(s, expected)
  499. def test_constructor_dtype_no_cast(self):
  500. # see gh-1572
  501. s = Series([1, 2, 3])
  502. s2 = Series(s, dtype=np.int64)
  503. s2[1] = 5
  504. assert s[1] == 5
  505. def test_constructor_datelike_coercion(self):
  506. # GH 9477
  507. # incorrectly inferring on dateimelike looking when object dtype is
  508. # specified
  509. s = Series([Timestamp('20130101'), 'NOV'], dtype=object)
  510. assert s.iloc[0] == Timestamp('20130101')
  511. assert s.iloc[1] == 'NOV'
  512. assert s.dtype == object
  513. # the dtype was being reset on the slicing and re-inferred to datetime
  514. # even thought the blocks are mixed
  515. belly = '216 3T19'.split()
  516. wing1 = '2T15 4H19'.split()
  517. wing2 = '416 4T20'.split()
  518. mat = pd.to_datetime('2016-01-22 2019-09-07'.split())
  519. df = pd.DataFrame(
  520. {'wing1': wing1,
  521. 'wing2': wing2,
  522. 'mat': mat}, index=belly)
  523. result = df.loc['3T19']
  524. assert result.dtype == object
  525. result = df.loc['216']
  526. assert result.dtype == object
  527. def test_constructor_datetimes_with_nulls(self):
  528. # gh-15869
  529. for arr in [np.array([None, None, None, None,
  530. datetime.now(), None]),
  531. np.array([None, None, datetime.now(), None])]:
  532. result = Series(arr)
  533. assert result.dtype == 'M8[ns]'
  534. def test_constructor_dtype_datetime64(self):
  535. s = Series(iNaT, dtype='M8[ns]', index=lrange(5))
  536. assert isna(s).all()
  537. # in theory this should be all nulls, but since
  538. # we are not specifying a dtype is ambiguous
  539. s = Series(iNaT, index=lrange(5))
  540. assert not isna(s).all()
  541. s = Series(nan, dtype='M8[ns]', index=lrange(5))
  542. assert isna(s).all()
  543. s = Series([datetime(2001, 1, 2, 0, 0), iNaT], dtype='M8[ns]')
  544. assert isna(s[1])
  545. assert s.dtype == 'M8[ns]'
  546. s = Series([datetime(2001, 1, 2, 0, 0), nan], dtype='M8[ns]')
  547. assert isna(s[1])
  548. assert s.dtype == 'M8[ns]'
  549. # GH3416
  550. dates = [
  551. np.datetime64(datetime(2013, 1, 1)),
  552. np.datetime64(datetime(2013, 1, 2)),
  553. np.datetime64(datetime(2013, 1, 3)),
  554. ]
  555. s = Series(dates)
  556. assert s.dtype == 'M8[ns]'
  557. s.iloc[0] = np.nan
  558. assert s.dtype == 'M8[ns]'
  559. # GH3414 related
  560. # msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to"
  561. # r" \[int32\]")
  562. # with pytest.raises(TypeError, match=msg):
  563. # Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]')
  564. pytest.raises(TypeError, lambda x: Series(
  565. Series(dates).astype('int') / 1000000, dtype='M8[ms]'))
  566. msg = (r"The 'datetime64' dtype has no unit\. Please pass in"
  567. r" 'datetime64\[ns\]' instead\.")
  568. with pytest.raises(ValueError, match=msg):
  569. Series(dates, dtype='datetime64')
  570. # invalid dates can be help as object
  571. result = Series([datetime(2, 1, 1)])
  572. assert result[0] == datetime(2, 1, 1, 0, 0)
  573. result = Series([datetime(3000, 1, 1)])
  574. assert result[0] == datetime(3000, 1, 1, 0, 0)
  575. # don't mix types
  576. result = Series([Timestamp('20130101'), 1], index=['a', 'b'])
  577. assert result['a'] == Timestamp('20130101')
  578. assert result['b'] == 1
  579. # GH6529
  580. # coerce datetime64 non-ns properly
  581. dates = date_range('01-Jan-2015', '01-Dec-2015', freq='M')
  582. values2 = dates.view(np.ndarray).astype('datetime64[ns]')
  583. expected = Series(values2, index=dates)
  584. for dtype in ['s', 'D', 'ms', 'us', 'ns']:
  585. values1 = dates.view(np.ndarray).astype('M8[{0}]'.format(dtype))
  586. result = Series(values1, dates)
  587. assert_series_equal(result, expected)
  588. # GH 13876
  589. # coerce to non-ns to object properly
  590. expected = Series(values2, index=dates, dtype=object)
  591. for dtype in ['s', 'D', 'ms', 'us', 'ns']:
  592. values1 = dates.view(np.ndarray).astype('M8[{0}]'.format(dtype))
  593. result = Series(values1, index=dates, dtype=object)
  594. assert_series_equal(result, expected)
  595. # leave datetime.date alone
  596. dates2 = np.array([d.date() for d in dates.to_pydatetime()],
  597. dtype=object)
  598. series1 = Series(dates2, dates)
  599. tm.assert_numpy_array_equal(series1.values, dates2)
  600. assert series1.dtype == object
  601. # these will correctly infer a datetime
  602. s = Series([None, pd.NaT, '2013-08-05 15:30:00.000001'])
  603. assert s.dtype == 'datetime64[ns]'
  604. s = Series([np.nan, pd.NaT, '2013-08-05 15:30:00.000001'])
  605. assert s.dtype == 'datetime64[ns]'
  606. s = Series([pd.NaT, None, '2013-08-05 15:30:00.000001'])
  607. assert s.dtype == 'datetime64[ns]'
  608. s = Series([pd.NaT, np.nan, '2013-08-05 15:30:00.000001'])
  609. assert s.dtype == 'datetime64[ns]'
  610. # tz-aware (UTC and other tz's)
  611. # GH 8411
  612. dr = date_range('20130101', periods=3)
  613. assert Series(dr).iloc[0].tz is None
  614. dr = date_range('20130101', periods=3, tz='UTC')
  615. assert str(Series(dr).iloc[0].tz) == 'UTC'
  616. dr = date_range('20130101', periods=3, tz='US/Eastern')
  617. assert str(Series(dr).iloc[0].tz) == 'US/Eastern'
  618. # non-convertible
  619. s = Series([1479596223000, -1479590, pd.NaT])
  620. assert s.dtype == 'object'
  621. assert s[2] is pd.NaT
  622. assert 'NaT' in str(s)
  623. # if we passed a NaT it remains
  624. s = Series([datetime(2010, 1, 1), datetime(2, 1, 1), pd.NaT])
  625. assert s.dtype == 'object'
  626. assert s[2] is pd.NaT
  627. assert 'NaT' in str(s)
  628. # if we passed a nan it remains
  629. s = Series([datetime(2010, 1, 1), datetime(2, 1, 1), np.nan])
  630. assert s.dtype == 'object'
  631. assert s[2] is np.nan
  632. assert 'NaN' in str(s)
  633. def test_constructor_with_datetime_tz(self):
  634. # 8260
  635. # support datetime64 with tz
  636. dr = date_range('20130101', periods=3, tz='US/Eastern')
  637. s = Series(dr)
  638. assert s.dtype.name == 'datetime64[ns, US/Eastern]'
  639. assert s.dtype == 'datetime64[ns, US/Eastern]'
  640. assert is_datetime64tz_dtype(s.dtype)
  641. assert 'datetime64[ns, US/Eastern]' in str(s)
  642. # export
  643. result = s.values
  644. assert isinstance(result, np.ndarray)
  645. assert result.dtype == 'datetime64[ns]'
  646. exp = pd.DatetimeIndex(result)
  647. exp = exp.tz_localize('UTC').tz_convert(tz=s.dt.tz)
  648. tm.assert_index_equal(dr, exp)
  649. # indexing
  650. result = s.iloc[0]
  651. assert result == Timestamp('2013-01-01 00:00:00-0500',
  652. tz='US/Eastern', freq='D')
  653. result = s[0]
  654. assert result == Timestamp('2013-01-01 00:00:00-0500',
  655. tz='US/Eastern', freq='D')
  656. result = s[Series([True, True, False], index=s.index)]
  657. assert_series_equal(result, s[0:2])
  658. result = s.iloc[0:1]
  659. assert_series_equal(result, Series(dr[0:1]))
  660. # concat
  661. result = pd.concat([s.iloc[0:1], s.iloc[1:]])
  662. assert_series_equal(result, s)
  663. # short str
  664. assert 'datetime64[ns, US/Eastern]' in str(s)
  665. # formatting with NaT
  666. result = s.shift()
  667. assert 'datetime64[ns, US/Eastern]' in str(result)
  668. assert 'NaT' in str(result)
  669. # long str
  670. t = Series(date_range('20130101', periods=1000, tz='US/Eastern'))
  671. assert 'datetime64[ns, US/Eastern]' in str(t)
  672. result = pd.DatetimeIndex(s, freq='infer')
  673. tm.assert_index_equal(result, dr)
  674. # inference
  675. s = Series([pd.Timestamp('2013-01-01 13:00:00-0800', tz='US/Pacific'),
  676. pd.Timestamp('2013-01-02 14:00:00-0800', tz='US/Pacific')])
  677. assert s.dtype == 'datetime64[ns, US/Pacific]'
  678. assert lib.infer_dtype(s, skipna=True) == 'datetime64'
  679. s = Series([pd.Timestamp('2013-01-01 13:00:00-0800', tz='US/Pacific'),
  680. pd.Timestamp('2013-01-02 14:00:00-0800', tz='US/Eastern')])
  681. assert s.dtype == 'object'
  682. assert lib.infer_dtype(s, skipna=True) == 'datetime'
  683. # with all NaT
  684. s = Series(pd.NaT, index=[0, 1], dtype='datetime64[ns, US/Eastern]')
  685. expected = Series(pd.DatetimeIndex(['NaT', 'NaT'], tz='US/Eastern'))
  686. assert_series_equal(s, expected)
  687. @pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
  688. @pytest.mark.parametrize("dtype", ["M8", "m8"])
  689. @pytest.mark.parametrize("unit", ['ns', 'us', 'ms', 's', 'h', 'm', 'D'])
  690. def test_construction_to_datetimelike_unit(self, arr_dtype, dtype, unit):
  691. # tests all units
  692. # gh-19223
  693. dtype = "{}[{}]".format(dtype, unit)
  694. arr = np.array([1, 2, 3], dtype=arr_dtype)
  695. s = Series(arr)
  696. result = s.astype(dtype)
  697. expected = Series(arr.astype(dtype))
  698. tm.assert_series_equal(result, expected)
  699. @pytest.mark.parametrize('arg',
  700. ['2013-01-01 00:00:00', pd.NaT, np.nan, None])
  701. def test_constructor_with_naive_string_and_datetimetz_dtype(self, arg):
  702. # GH 17415: With naive string
  703. result = Series([arg], dtype='datetime64[ns, CET]')
  704. expected = Series(pd.Timestamp(arg)).dt.tz_localize('CET')
  705. assert_series_equal(result, expected)
  706. def test_construction_interval(self):
  707. # construction from interval & array of intervals
  708. index = IntervalIndex.from_breaks(np.arange(3), closed='right')
  709. result = Series(index)
  710. repr(result)
  711. str(result)
  712. tm.assert_index_equal(Index(result.values), index)
  713. result = Series(index.values)
  714. tm.assert_index_equal(Index(result.values), index)
  715. def test_construction_consistency(self):
  716. # make sure that we are not re-localizing upon construction
  717. # GH 14928
  718. s = Series(pd.date_range('20130101', periods=3, tz='US/Eastern'))
  719. result = Series(s, dtype=s.dtype)
  720. tm.assert_series_equal(result, s)
  721. result = Series(s.dt.tz_convert('UTC'), dtype=s.dtype)
  722. tm.assert_series_equal(result, s)
  723. result = Series(s.values, dtype=s.dtype)
  724. tm.assert_series_equal(result, s)
  725. def test_constructor_infer_period(self):
  726. data = [pd.Period('2000', 'D'), pd.Period('2001', 'D'), None]
  727. result = pd.Series(data)
  728. expected = pd.Series(period_array(data))
  729. tm.assert_series_equal(result, expected)
  730. assert result.dtype == 'Period[D]'
  731. data = np.asarray(data, dtype=object)
  732. tm.assert_series_equal(result, expected)
  733. assert result.dtype == 'Period[D]'
  734. def test_constructor_period_incompatible_frequency(self):
  735. data = [pd.Period('2000', 'D'), pd.Period('2001', 'A')]
  736. result = pd.Series(data)
  737. assert result.dtype == object
  738. assert result.tolist() == data
  739. def test_constructor_periodindex(self):
  740. # GH7932
  741. # converting a PeriodIndex when put in a Series
  742. pi = period_range('20130101', periods=5, freq='D')
  743. s = Series(pi)
  744. assert s.dtype == 'Period[D]'
  745. expected = Series(pi.astype(object))
  746. assert_series_equal(s, expected)
  747. def test_constructor_dict(self):
  748. d = {'a': 0., 'b': 1., 'c': 2.}
  749. result = Series(d, index=['b', 'c', 'd', 'a'])
  750. expected = Series([1, 2, nan, 0], index=['b', 'c', 'd', 'a'])
  751. assert_series_equal(result, expected)
  752. pidx = tm.makePeriodIndex(100)
  753. d = {pidx[0]: 0, pidx[1]: 1}
  754. result = Series(d, index=pidx)
  755. expected = Series(np.nan, pidx)
  756. expected.iloc[0] = 0
  757. expected.iloc[1] = 1
  758. assert_series_equal(result, expected)
  759. def test_constructor_dict_order(self):
  760. # GH19018
  761. # initialization ordering: by insertion order if python>= 3.6, else
  762. # order by value
  763. d = {'b': 1, 'a': 0, 'c': 2}
  764. result = Series(d)
  765. if PY36:
  766. expected = Series([1, 0, 2], index=list('bac'))
  767. else:
  768. expected = Series([0, 1, 2], index=list('abc'))
  769. tm.assert_series_equal(result, expected)
  770. @pytest.mark.parametrize("value", [2, np.nan, None, float('nan')])
  771. def test_constructor_dict_nan_key(self, value):
  772. # GH 18480
  773. d = {1: 'a', value: 'b', float('nan'): 'c', 4: 'd'}
  774. result = Series(d).sort_values()
  775. expected = Series(['a', 'b', 'c', 'd'], index=[1, value, np.nan, 4])
  776. assert_series_equal(result, expected)
  777. # MultiIndex:
  778. d = {(1, 1): 'a', (2, np.nan): 'b', (3, value): 'c'}
  779. result = Series(d).sort_values()
  780. expected = Series(['a', 'b', 'c'],
  781. index=Index([(1, 1), (2, np.nan), (3, value)]))
  782. assert_series_equal(result, expected)
  783. def test_constructor_dict_datetime64_index(self):
  784. # GH 9456
  785. dates_as_str = ['1984-02-19', '1988-11-06', '1989-12-03', '1990-03-15']
  786. values = [42544017.198965244, 1234565, 40512335.181958228, -1]
  787. def create_data(constructor):
  788. return dict(zip((constructor(x) for x in dates_as_str), values))
  789. data_datetime64 = create_data(np.datetime64)
  790. data_datetime = create_data(lambda x: datetime.strptime(x, '%Y-%m-%d'))
  791. data_Timestamp = create_data(Timestamp)
  792. expected = Series(values, (Timestamp(x) for x in dates_as_str))
  793. result_datetime64 = Series(data_datetime64)
  794. result_datetime = Series(data_datetime)
  795. result_Timestamp = Series(data_Timestamp)
  796. assert_series_equal(result_datetime64, expected)
  797. assert_series_equal(result_datetime, expected)
  798. assert_series_equal(result_Timestamp, expected)
  799. def test_constructor_list_of_tuples(self):
  800. data = [(1, 1), (2, 2), (2, 3)]
  801. s = Series(data)
  802. assert list(s) == data
  803. def test_constructor_tuple_of_tuples(self):
  804. data = ((1, 1), (2, 2), (2, 3))
  805. s = Series(data)
  806. assert tuple(s) == data
  807. def test_constructor_dict_of_tuples(self):
  808. data = {(1, 2): 3,
  809. (None, 5): 6}
  810. result = Series(data).sort_values()
  811. expected = Series([3, 6],
  812. index=MultiIndex.from_tuples([(1, 2), (None, 5)]))
  813. tm.assert_series_equal(result, expected)
  814. def test_constructor_set(self):
  815. values = {1, 2, 3, 4, 5}
  816. with pytest.raises(TypeError, match="'set' type is unordered"):
  817. Series(values)
  818. values = frozenset(values)
  819. with pytest.raises(TypeError, match="'frozenset' type is unordered"):
  820. Series(values)
  821. # https://github.com/pandas-dev/pandas/issues/22698
  822. @pytest.mark.filterwarnings("ignore:elementwise comparison:FutureWarning")
  823. def test_fromDict(self):
  824. data = {'a': 0, 'b': 1, 'c': 2, 'd': 3}
  825. series = Series(data)
  826. assert tm.is_sorted(series.index)
  827. data = {'a': 0, 'b': '1', 'c': '2', 'd': datetime.now()}
  828. series = Series(data)
  829. assert series.dtype == np.object_
  830. data = {'a': 0, 'b': '1', 'c': '2', 'd': '3'}
  831. series = Series(data)
  832. assert series.dtype == np.object_
  833. data = {'a': '0', 'b': '1'}
  834. series = Series(data, dtype=float)
  835. assert series.dtype == np.float64
  836. def test_fromValue(self, datetime_series):
  837. nans = Series(np.NaN, index=datetime_series.index)
  838. assert nans.dtype == np.float_
  839. assert len(nans) == len(datetime_series)
  840. strings = Series('foo', index=datetime_series.index)
  841. assert strings.dtype == np.object_
  842. assert len(strings) == len(datetime_series)
  843. d = datetime.now()
  844. dates = Series(d, index=datetime_series.index)
  845. assert dates.dtype == 'M8[ns]'
  846. assert len(dates) == len(datetime_series)
  847. # GH12336
  848. # Test construction of categorical series from value
  849. categorical = Series(0, index=datetime_series.index, dtype="category")
  850. expected = Series(0, index=datetime_series.index).astype("category")
  851. assert categorical.dtype == 'category'
  852. assert len(categorical) == len(datetime_series)
  853. tm.assert_series_equal(categorical, expected)
  854. def test_constructor_dtype_timedelta64(self):
  855. # basic
  856. td = Series([timedelta(days=i) for i in range(3)])
  857. assert td.dtype == 'timedelta64[ns]'
  858. td = Series([timedelta(days=1)])
  859. assert td.dtype == 'timedelta64[ns]'
  860. td = Series([timedelta(days=1), timedelta(days=2), np.timedelta64(
  861. 1, 's')])
  862. assert td.dtype == 'timedelta64[ns]'
  863. # mixed with NaT
  864. td = Series([timedelta(days=1), NaT], dtype='m8[ns]')
  865. assert td.dtype == 'timedelta64[ns]'
  866. td = Series([timedelta(days=1), np.nan], dtype='m8[ns]')
  867. assert td.dtype == 'timedelta64[ns]'
  868. td = Series([np.timedelta64(300000000), pd.NaT], dtype='m8[ns]')
  869. assert td.dtype == 'timedelta64[ns]'
  870. # improved inference
  871. # GH5689
  872. td = Series([np.timedelta64(300000000), NaT])
  873. assert td.dtype == 'timedelta64[ns]'
  874. # because iNaT is int, not coerced to timedelta
  875. td = Series([np.timedelta64(300000000), iNaT])
  876. assert td.dtype == 'object'
  877. td = Series([np.timedelta64(300000000), np.nan])
  878. assert td.dtype == 'timedelta64[ns]'
  879. td = Series([pd.NaT, np.timedelta64(300000000)])
  880. assert td.dtype == 'timedelta64[ns]'
  881. td = Series([np.timedelta64(1, 's')])
  882. assert td.dtype == 'timedelta64[ns]'
  883. # these are frequency conversion astypes
  884. # for t in ['s', 'D', 'us', 'ms']:
  885. # pytest.raises(TypeError, td.astype, 'm8[%s]' % t)
  886. # valid astype
  887. td.astype('int64')
  888. # invalid casting
  889. msg = (r"cannot astype a timedelta from \[timedelta64\[ns\]\] to"
  890. r" \[int32\]")
  891. with pytest.raises(TypeError, match=msg):
  892. td.astype('int32')
  893. # this is an invalid casting
  894. msg = "Could not convert object to NumPy timedelta"
  895. with pytest.raises(ValueError, match=msg):
  896. Series([timedelta(days=1), 'foo'], dtype='m8[ns]')
  897. # leave as object here
  898. td = Series([timedelta(days=i) for i in range(3)] + ['foo'])
  899. assert td.dtype == 'object'
  900. # these will correctly infer a timedelta
  901. s = Series([None, pd.NaT, '1 Day'])
  902. assert s.dtype == 'timedelta64[ns]'
  903. s = Series([np.nan, pd.NaT, '1 Day'])
  904. assert s.dtype == 'timedelta64[ns]'
  905. s = Series([pd.NaT, None, '1 Day'])
  906. assert s.dtype == 'timedelta64[ns]'
  907. s = Series([pd.NaT, np.nan, '1 Day'])
  908. assert s.dtype == 'timedelta64[ns]'
  909. # GH 16406
  910. def test_constructor_mixed_tz(self):
  911. s = Series([Timestamp('20130101'),
  912. Timestamp('20130101', tz='US/Eastern')])
  913. expected = Series([Timestamp('20130101'),
  914. Timestamp('20130101', tz='US/Eastern')],
  915. dtype='object')
  916. assert_series_equal(s, expected)
  917. def test_NaT_scalar(self):
  918. series = Series([0, 1000, 2000, iNaT], dtype='M8[ns]')
  919. val = series[3]
  920. assert isna(val)
  921. series[2] = val
  922. assert isna(series[2])
  923. def test_NaT_cast(self):
  924. # GH10747
  925. result = Series([np.nan]).astype('M8[ns]')
  926. expected = Series([NaT])
  927. assert_series_equal(result, expected)
  928. def test_constructor_name_hashable(self):
  929. for n in [777, 777., 'name', datetime(2001, 11, 11), (1, ), u"\u05D0"]:
  930. for data in [[1, 2, 3], np.ones(3), {'a': 0, 'b': 1}]:
  931. s = Series(data, name=n)
  932. assert s.name == n
  933. def test_constructor_name_unhashable(self):
  934. msg = r"Series\.name must be a hashable type"
  935. for n in [['name_list'], np.ones(2), {1: 2}]:
  936. for data in [['name_list'], np.ones(2), {1: 2}]:
  937. with pytest.raises(TypeError, match=msg):
  938. Series(data, name=n)
  939. def test_auto_conversion(self):
  940. series = Series(list(date_range('1/1/2000', periods=10)))
  941. assert series.dtype == 'M8[ns]'
  942. def test_convert_non_ns(self):
  943. # convert from a numpy array of non-ns timedelta64
  944. arr = np.array([1, 2, 3], dtype='timedelta64[s]')
  945. s = Series(arr)
  946. expected = Series(pd.timedelta_range('00:00:01', periods=3, freq='s'))
  947. assert_series_equal(s, expected)
  948. # convert from a numpy array of non-ns datetime64
  949. # note that creating a numpy datetime64 is in LOCAL time!!!!
  950. # seems to work for M8[D], but not for M8[s]
  951. s = Series(np.array(['2013-01-01', '2013-01-02',
  952. '2013-01-03'], dtype='datetime64[D]'))
  953. assert_series_equal(s, Series(date_range('20130101', periods=3,
  954. freq='D')))
  955. # s = Series(np.array(['2013-01-01 00:00:01','2013-01-01
  956. # 00:00:02','2013-01-01 00:00:03'],dtype='datetime64[s]'))
  957. # assert_series_equal(s,date_range('20130101
  958. # 00:00:01',period=3,freq='s'))
  959. @pytest.mark.parametrize(
  960. "index",
  961. [
  962. date_range('1/1/2000', periods=10),
  963. timedelta_range('1 day', periods=10),
  964. period_range('2000-Q1', periods=10, freq='Q')],
  965. ids=lambda x: type(x).__name__)
  966. def test_constructor_cant_cast_datetimelike(self, index):
  967. # floats are not ok
  968. msg = "Cannot cast {}.*? to ".format(
  969. # strip Index to convert PeriodIndex -> Period
  970. # We don't care whether the error message says
  971. # PeriodIndex or PeriodArray
  972. type(index).__name__.rstrip("Index")
  973. )
  974. with pytest.raises(TypeError, match=msg):
  975. Series(index, dtype=float)
  976. # ints are ok
  977. # we test with np.int64 to get similar results on
  978. # windows / 32-bit platforms
  979. result = Series(index, dtype=np.int64)
  980. expected = Series(index.astype(np.int64))
  981. tm.assert_series_equal(result, expected)
  982. @pytest.mark.parametrize(
  983. "index",
  984. [
  985. date_range('1/1/2000', periods=10),
  986. timedelta_range('1 day', periods=10),
  987. period_range('2000-Q1', periods=10, freq='Q')],
  988. ids=lambda x: type(x).__name__)
  989. def test_constructor_cast_object(self, index):
  990. s = Series(index, dtype=object)
  991. exp = Series(index).astype(object)
  992. tm.assert_series_equal(s, exp)
  993. s = Series(pd.Index(index, dtype=object), dtype=object)
  994. exp = Series(index).astype(object)
  995. tm.assert_series_equal(s, exp)
  996. s = Series(index.astype(object), dtype=object)
  997. exp = Series(index).astype(object)
  998. tm.assert_series_equal(s, exp)
  999. @pytest.mark.parametrize("dtype", [
  1000. np.datetime64,
  1001. np.timedelta64,
  1002. ])
  1003. def test_constructor_generic_timestamp_no_frequency(self, dtype):
  1004. # see gh-15524, gh-15987
  1005. msg = "dtype has no unit. Please pass in"
  1006. with pytest.raises(ValueError, match=msg):
  1007. Series([], dtype=dtype)
  1008. @pytest.mark.parametrize("dtype,msg", [
  1009. ("m8[ps]", "cannot convert timedeltalike"),
  1010. ("M8[ps]", "cannot convert datetimelike"),
  1011. ])
  1012. def test_constructor_generic_timestamp_bad_frequency(self, dtype, msg):
  1013. # see gh-15524, gh-15987
  1014. with pytest.raises(TypeError, match=msg):
  1015. Series([], dtype=dtype)
  1016. @pytest.mark.parametrize('dtype', [None, 'uint8', 'category'])
  1017. def test_constructor_range_dtype(self, dtype):
  1018. # GH 16804
  1019. expected = Series([0, 1, 2, 3, 4], dtype=dtype or 'int64')
  1020. result = Series(range(5), dtype=dtype)
  1021. tm.assert_series_equal(result, expected)
  1022. def test_constructor_tz_mixed_data(self):
  1023. # GH 13051
  1024. dt_list = [Timestamp('2016-05-01 02:03:37'),
  1025. Timestamp('2016-04-30 19:03:37-0700', tz='US/Pacific')]
  1026. result = Series(dt_list)
  1027. expected = Series(dt_list, dtype=object)
  1028. tm.assert_series_equal(result, expected)