test_constructors.py 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. from collections import OrderedDict
  2. from collections.abc import Iterator
  3. from datetime import (
  4. datetime,
  5. timedelta,
  6. )
  7. from dateutil.tz import tzoffset
  8. import numpy as np
  9. from numpy import ma
  10. import pytest
  11. from pandas._libs import (
  12. iNaT,
  13. lib,
  14. )
  15. from pandas.compat import HAS_PYARROW
  16. from pandas.compat.numpy import np_version_gt2
  17. from pandas.errors import IntCastingNaNError
  18. import pandas.util._test_decorators as td
  19. from pandas.core.dtypes.dtypes import CategoricalDtype
  20. import pandas as pd
  21. from pandas import (
  22. Categorical,
  23. DataFrame,
  24. DatetimeIndex,
  25. DatetimeTZDtype,
  26. Index,
  27. Interval,
  28. IntervalIndex,
  29. MultiIndex,
  30. NaT,
  31. Period,
  32. RangeIndex,
  33. Series,
  34. Timestamp,
  35. date_range,
  36. isna,
  37. period_range,
  38. timedelta_range,
  39. )
  40. import pandas._testing as tm
  41. from pandas.core.arrays import (
  42. IntegerArray,
  43. IntervalArray,
  44. period_array,
  45. )
  46. from pandas.core.internals.blocks import NumpyBlock
  47. class TestSeriesConstructors:
  48. def test_from_ints_with_non_nano_dt64_dtype(self, index_or_series):
  49. values = np.arange(10)
  50. res = index_or_series(values, dtype="M8[s]")
  51. expected = index_or_series(values.astype("M8[s]"))
  52. tm.assert_equal(res, expected)
  53. res = index_or_series(list(values), dtype="M8[s]")
  54. tm.assert_equal(res, expected)
  55. def test_from_na_value_and_interval_of_datetime_dtype(self):
  56. # GH#41805
  57. ser = Series([None], dtype="interval[datetime64[ns]]")
  58. assert ser.isna().all()
  59. assert ser.dtype == "interval[datetime64[ns], right]"
  60. def test_infer_with_date_and_datetime(self):
  61. # GH#49341 pre-2.0 we inferred datetime-and-date to datetime64, which
  62. # was inconsistent with Index behavior
  63. ts = Timestamp(2016, 1, 1)
  64. vals = [ts.to_pydatetime(), ts.date()]
  65. ser = Series(vals)
  66. expected = Series(vals, dtype=object)
  67. tm.assert_series_equal(ser, expected)
  68. idx = Index(vals)
  69. expected = Index(vals, dtype=object)
  70. tm.assert_index_equal(idx, expected)
  71. def test_unparsable_strings_with_dt64_dtype(self):
  72. # pre-2.0 these would be silently ignored and come back with object dtype
  73. vals = ["aa"]
  74. msg = "^Unknown datetime string format, unable to parse: aa, at position 0$"
  75. with pytest.raises(ValueError, match=msg):
  76. Series(vals, dtype="datetime64[ns]")
  77. with pytest.raises(ValueError, match=msg):
  78. Series(np.array(vals, dtype=object), dtype="datetime64[ns]")
  79. @pytest.mark.parametrize(
  80. "constructor",
  81. [
  82. # NOTE: some overlap with test_constructor_empty but that test does not
  83. # test for None or an empty generator.
  84. # test_constructor_pass_none tests None but only with the index also
  85. # passed.
  86. (lambda idx: Series(index=idx)),
  87. (lambda idx: Series(None, index=idx)),
  88. (lambda idx: Series({}, index=idx)),
  89. (lambda idx: Series((), index=idx)),
  90. (lambda idx: Series([], index=idx)),
  91. (lambda idx: Series((_ for _ in []), index=idx)),
  92. (lambda idx: Series(data=None, index=idx)),
  93. (lambda idx: Series(data={}, index=idx)),
  94. (lambda idx: Series(data=(), index=idx)),
  95. (lambda idx: Series(data=[], index=idx)),
  96. (lambda idx: Series(data=(_ for _ in []), index=idx)),
  97. ],
  98. )
  99. @pytest.mark.parametrize("empty_index", [None, []])
  100. def test_empty_constructor(self, constructor, empty_index):
  101. # GH 49573 (addition of empty_index parameter)
  102. expected = Series(index=empty_index)
  103. result = constructor(empty_index)
  104. assert result.dtype == object
  105. assert len(result.index) == 0
  106. tm.assert_series_equal(result, expected, check_index_type=True)
  107. def test_invalid_dtype(self):
  108. # GH15520
  109. msg = "not understood"
  110. invalid_list = [Timestamp, "Timestamp", list]
  111. for dtype in invalid_list:
  112. with pytest.raises(TypeError, match=msg):
  113. Series([], name="time", dtype=dtype)
  114. def test_invalid_compound_dtype(self):
  115. # GH#13296
  116. c_dtype = np.dtype([("a", "i8"), ("b", "f4")])
  117. cdt_arr = np.array([(1, 0.4), (256, -13)], dtype=c_dtype)
  118. with pytest.raises(ValueError, match="Use DataFrame instead"):
  119. Series(cdt_arr, index=["A", "B"])
  120. def test_scalar_conversion(self):
  121. # Pass in scalar is disabled
  122. scalar = Series(0.5)
  123. assert not isinstance(scalar, float)
  124. def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
  125. # GH 28401
  126. ea_scalar, ea_dtype = ea_scalar_and_dtype
  127. ser = Series(ea_scalar, index=range(3))
  128. expected = Series([ea_scalar] * 3, dtype=ea_dtype)
  129. assert ser.dtype == ea_dtype
  130. tm.assert_series_equal(ser, expected)
  131. def test_constructor(self, datetime_series, using_infer_string):
  132. empty_series = Series()
  133. assert datetime_series.index._is_all_dates
  134. # Pass in Series
  135. derived = Series(datetime_series)
  136. assert derived.index._is_all_dates
  137. tm.assert_index_equal(derived.index, datetime_series.index)
  138. # Ensure new index is not created
  139. assert id(datetime_series.index) == id(derived.index)
  140. # Mixed type Series
  141. mixed = Series(["hello", np.nan], index=[0, 1])
  142. assert mixed.dtype == np.object_ if not using_infer_string else "str"
  143. assert np.isnan(mixed[1])
  144. assert not empty_series.index._is_all_dates
  145. assert not Series().index._is_all_dates
  146. # exception raised is of type ValueError GH35744
  147. with pytest.raises(
  148. ValueError,
  149. match=r"Data must be 1-dimensional, got ndarray of shape \(3, 3\) instead",
  150. ):
  151. Series(np.random.default_rng(2).standard_normal((3, 3)), index=np.arange(3))
  152. mixed.name = "Series"
  153. rs = Series(mixed).name
  154. xp = "Series"
  155. assert rs == xp
  156. # raise on MultiIndex GH4187
  157. m = MultiIndex.from_arrays([[1, 2], [3, 4]])
  158. msg = "initializing a Series from a MultiIndex is not supported"
  159. with pytest.raises(NotImplementedError, match=msg):
  160. Series(m)
  161. def test_constructor_index_ndim_gt_1_raises(self):
  162. # GH#18579
  163. df = DataFrame([[1, 2], [3, 4], [5, 6]], index=[3, 6, 9])
  164. with pytest.raises(ValueError, match="Index data must be 1-dimensional"):
  165. Series([1, 3, 2], index=df)
  166. @pytest.mark.parametrize("input_class", [list, dict, OrderedDict])
  167. def test_constructor_empty(self, input_class, using_infer_string):
  168. empty = Series()
  169. empty2 = Series(input_class())
  170. # these are Index() and RangeIndex() which don't compare type equal
  171. # but are just .equals
  172. tm.assert_series_equal(empty, empty2, check_index_type=False)
  173. # With explicit dtype:
  174. empty = Series(dtype="float64")
  175. empty2 = Series(input_class(), dtype="float64")
  176. tm.assert_series_equal(empty, empty2, check_index_type=False)
  177. # GH 18515 : with dtype=category:
  178. empty = Series(dtype="category")
  179. empty2 = Series(input_class(), dtype="category")
  180. tm.assert_series_equal(empty, empty2, check_index_type=False)
  181. if input_class is not list:
  182. # With index:
  183. empty = Series(index=range(10))
  184. empty2 = Series(input_class(), index=range(10))
  185. tm.assert_series_equal(empty, empty2)
  186. # With index and dtype float64:
  187. empty = Series(np.nan, index=range(10))
  188. empty2 = Series(input_class(), index=range(10), dtype="float64")
  189. tm.assert_series_equal(empty, empty2)
  190. # GH 19853 : with empty string, index and dtype str
  191. empty = Series("", dtype=str, index=range(3))
  192. if using_infer_string:
  193. empty2 = Series("", index=range(3), dtype="str")
  194. else:
  195. empty2 = Series("", index=range(3))
  196. tm.assert_series_equal(empty, empty2)
  197. @pytest.mark.parametrize("input_arg", [np.nan, float("nan")])
  198. def test_constructor_nan(self, input_arg):
  199. empty = Series(dtype="float64", index=range(10))
  200. empty2 = Series(input_arg, index=range(10))
  201. tm.assert_series_equal(empty, empty2, check_index_type=False)
  202. @pytest.mark.parametrize(
  203. "dtype",
  204. ["f8", "i8", "M8[ns]", "m8[ns]", "category", "object", "datetime64[ns, UTC]"],
  205. )
  206. @pytest.mark.parametrize("index", [None, Index([])])
  207. def test_constructor_dtype_only(self, dtype, index):
  208. # GH-20865
  209. result = Series(dtype=dtype, index=index)
  210. assert result.dtype == dtype
  211. assert len(result) == 0
  212. def test_constructor_no_data_index_order(self):
  213. result = Series(index=["b", "a", "c"])
  214. assert result.index.tolist() == ["b", "a", "c"]
  215. def test_constructor_no_data_string_type(self):
  216. # GH 22477
  217. result = Series(index=[1], dtype=str)
  218. assert np.isnan(result.iloc[0])
  219. @pytest.mark.parametrize("item", ["entry", "ѐ", 13])
  220. def test_constructor_string_element_string_type(self, item):
  221. # GH 22477
  222. result = Series(item, index=[1], dtype=str)
  223. assert result.iloc[0] == str(item)
  224. def test_constructor_dtype_str_na_values(self, string_dtype):
  225. # https://github.com/pandas-dev/pandas/issues/21083
  226. ser = Series(["x", None], dtype=string_dtype)
  227. result = ser.isna()
  228. expected = Series([False, True])
  229. tm.assert_series_equal(result, expected)
  230. assert ser.iloc[1] is None
  231. ser = Series(["x", np.nan], dtype=string_dtype)
  232. assert np.isnan(ser.iloc[1])
  233. def test_constructor_series(self):
  234. index1 = ["d", "b", "a", "c"]
  235. index2 = sorted(index1)
  236. s1 = Series([4, 7, -5, 3], index=index1)
  237. s2 = Series(s1, index=index2)
  238. tm.assert_series_equal(s2, s1.sort_index())
  239. def test_constructor_iterable(self):
  240. # GH 21987
  241. class Iter:
  242. def __iter__(self) -> Iterator:
  243. yield from range(10)
  244. expected = Series(list(range(10)), dtype="int64")
  245. result = Series(Iter(), dtype="int64")
  246. tm.assert_series_equal(result, expected)
  247. def test_constructor_sequence(self):
  248. # GH 21987
  249. expected = Series(list(range(10)), dtype="int64")
  250. result = Series(range(10), dtype="int64")
  251. tm.assert_series_equal(result, expected)
  252. def test_constructor_single_str(self):
  253. # GH 21987
  254. expected = Series(["abc"])
  255. result = Series("abc")
  256. tm.assert_series_equal(result, expected)
  257. def test_constructor_list_like(self):
  258. # make sure that we are coercing different
  259. # list-likes to standard dtypes and not
  260. # platform specific
  261. expected = Series([1, 2, 3], dtype="int64")
  262. for obj in [[1, 2, 3], (1, 2, 3), np.array([1, 2, 3], dtype="int64")]:
  263. result = Series(obj, index=[0, 1, 2])
  264. tm.assert_series_equal(result, expected)
  265. def test_constructor_boolean_index(self):
  266. # GH#18579
  267. s1 = Series([1, 2, 3], index=[4, 5, 6])
  268. index = s1 == 2
  269. result = Series([1, 3, 2], index=index)
  270. expected = Series([1, 3, 2], index=[False, True, False])
  271. tm.assert_series_equal(result, expected)
  272. @pytest.mark.parametrize("dtype", ["bool", "int32", "int64", "float64"])
  273. def test_constructor_index_dtype(self, dtype):
  274. # GH 17088
  275. s = Series(Index([0, 2, 4]), dtype=dtype)
  276. assert s.dtype == dtype
  277. @pytest.mark.parametrize(
  278. "input_vals",
  279. [
  280. ([1, 2]),
  281. (["1", "2"]),
  282. (list(date_range("1/1/2011", periods=2, freq="h"))),
  283. (list(date_range("1/1/2011", periods=2, freq="h", tz="US/Eastern"))),
  284. ([Interval(left=0, right=5)]),
  285. ],
  286. )
  287. def test_constructor_list_str(self, input_vals, string_dtype):
  288. # GH 16605
  289. # Ensure that data elements from a list are converted to strings
  290. # when dtype is str, 'str', or 'U'
  291. result = Series(input_vals, dtype=string_dtype)
  292. expected = Series(input_vals).astype(string_dtype)
  293. tm.assert_series_equal(result, expected)
  294. def test_constructor_list_str_na(self, string_dtype):
  295. result = Series([1.0, 2.0, np.nan], dtype=string_dtype)
  296. expected = Series(["1.0", "2.0", np.nan], dtype=object)
  297. tm.assert_series_equal(result, expected)
  298. assert np.isnan(result[2])
  299. def test_constructor_generator(self):
  300. gen = (i for i in range(10))
  301. result = Series(gen)
  302. exp = Series(range(10))
  303. tm.assert_series_equal(result, exp)
  304. # same but with non-default index
  305. gen = (i for i in range(10))
  306. result = Series(gen, index=range(10, 20))
  307. exp.index = range(10, 20)
  308. tm.assert_series_equal(result, exp)
  309. def test_constructor_map(self):
  310. # GH8909
  311. m = (x for x in range(10))
  312. result = Series(m)
  313. exp = Series(range(10))
  314. tm.assert_series_equal(result, exp)
  315. # same but with non-default index
  316. m = (x for x in range(10))
  317. result = Series(m, index=range(10, 20))
  318. exp.index = range(10, 20)
  319. tm.assert_series_equal(result, exp)
  320. def test_constructor_categorical(self):
  321. cat = Categorical([0, 1, 2, 0, 1, 2], ["a", "b", "c"])
  322. res = Series(cat)
  323. tm.assert_categorical_equal(res.values, cat)
  324. # can cast to a new dtype
  325. result = Series(Categorical([1, 2, 3]), dtype="int64")
  326. expected = Series([1, 2, 3], dtype="int64")
  327. tm.assert_series_equal(result, expected)
  328. def test_construct_from_categorical_with_dtype(self):
  329. # GH12574
  330. ser = Series(Categorical([1, 2, 3]), dtype="category")
  331. assert isinstance(ser.dtype, CategoricalDtype)
  332. def test_construct_intlist_values_category_dtype(self):
  333. ser = Series([1, 2, 3], dtype="category")
  334. assert isinstance(ser.dtype, CategoricalDtype)
  335. def test_constructor_categorical_with_coercion(self):
  336. factor = Categorical(["a", "b", "b", "a", "a", "c", "c", "c"])
  337. # test basic creation / coercion of categoricals
  338. s = Series(factor, name="A")
  339. assert s.dtype == "category"
  340. assert len(s) == len(factor)
  341. # in a frame
  342. df = DataFrame({"A": factor})
  343. result = df["A"]
  344. tm.assert_series_equal(result, s)
  345. result = df.iloc[:, 0]
  346. tm.assert_series_equal(result, s)
  347. assert len(df) == len(factor)
  348. df = DataFrame({"A": s})
  349. result = df["A"]
  350. tm.assert_series_equal(result, s)
  351. assert len(df) == len(factor)
  352. # multiples
  353. df = DataFrame({"A": s, "B": s, "C": 1})
  354. result1 = df["A"]
  355. result2 = df["B"]
  356. tm.assert_series_equal(result1, s)
  357. tm.assert_series_equal(result2, s, check_names=False)
  358. assert result2.name == "B"
  359. assert len(df) == len(factor)
  360. def test_constructor_categorical_with_coercion2(self):
  361. # GH8623
  362. x = DataFrame(
  363. [[1, "John P. Doe"], [2, "Jane Dove"], [1, "John P. Doe"]],
  364. columns=["person_id", "person_name"],
  365. )
  366. x["person_name"] = Categorical(x.person_name) # doing this breaks transform
  367. expected = x.iloc[0].person_name
  368. result = x.person_name.iloc[0]
  369. assert result == expected
  370. result = x.person_name[0]
  371. assert result == expected
  372. result = x.person_name.loc[0]
  373. assert result == expected
  374. def test_constructor_series_to_categorical(self):
  375. # see GH#16524: test conversion of Series to Categorical
  376. series = Series(["a", "b", "c"])
  377. result = Series(series, dtype="category")
  378. expected = Series(["a", "b", "c"], dtype="category")
  379. tm.assert_series_equal(result, expected)
  380. def test_constructor_categorical_dtype(self):
  381. result = Series(
  382. ["a", "b"], dtype=CategoricalDtype(["a", "b", "c"], ordered=True)
  383. )
  384. assert isinstance(result.dtype, CategoricalDtype)
  385. tm.assert_index_equal(result.cat.categories, Index(["a", "b", "c"]))
  386. assert result.cat.ordered
  387. result = Series(["a", "b"], dtype=CategoricalDtype(["b", "a"]))
  388. assert isinstance(result.dtype, CategoricalDtype)
  389. tm.assert_index_equal(result.cat.categories, Index(["b", "a"]))
  390. assert result.cat.ordered is False
  391. # GH 19565 - Check broadcasting of scalar with Categorical dtype
  392. result = Series(
  393. "a", index=[0, 1], dtype=CategoricalDtype(["a", "b"], ordered=True)
  394. )
  395. expected = Series(
  396. ["a", "a"], index=[0, 1], dtype=CategoricalDtype(["a", "b"], ordered=True)
  397. )
  398. tm.assert_series_equal(result, expected)
  399. def test_constructor_categorical_string(self):
  400. # GH 26336: the string 'category' maintains existing CategoricalDtype
  401. cdt = CategoricalDtype(categories=list("dabc"), ordered=True)
  402. expected = Series(list("abcabc"), dtype=cdt)
  403. # Series(Categorical, dtype='category') keeps existing dtype
  404. cat = Categorical(list("abcabc"), dtype=cdt)
  405. result = Series(cat, dtype="category")
  406. tm.assert_series_equal(result, expected)
  407. # Series(Series[Categorical], dtype='category') keeps existing dtype
  408. result = Series(result, dtype="category")
  409. tm.assert_series_equal(result, expected)
  410. def test_categorical_sideeffects_free(self):
  411. # Passing a categorical to a Series and then changing values in either
  412. # the series or the categorical should not change the values in the
  413. # other one, IF you specify copy!
  414. cat = Categorical(["a", "b", "c", "a"])
  415. s = Series(cat, copy=True)
  416. assert s.cat is not cat
  417. s = s.cat.rename_categories([1, 2, 3])
  418. exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
  419. exp_cat = np.array(["a", "b", "c", "a"], dtype=np.object_)
  420. tm.assert_numpy_array_equal(s.__array__(), exp_s)
  421. tm.assert_numpy_array_equal(cat.__array__(), exp_cat)
  422. # setting
  423. s[0] = 2
  424. exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
  425. tm.assert_numpy_array_equal(s.__array__(), exp_s2)
  426. tm.assert_numpy_array_equal(cat.__array__(), exp_cat)
  427. # however, copy is False by default
  428. # so this WILL change values
  429. cat = Categorical(["a", "b", "c", "a"])
  430. s = Series(cat, copy=False)
  431. assert s.values is cat
  432. s = s.cat.rename_categories([1, 2, 3])
  433. assert s.values is not cat
  434. exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
  435. tm.assert_numpy_array_equal(s.__array__(), exp_s)
  436. s[0] = 2
  437. exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
  438. tm.assert_numpy_array_equal(s.__array__(), exp_s2)
  439. def test_unordered_compare_equal(self):
  440. left = Series(["a", "b", "c"], dtype=CategoricalDtype(["a", "b"]))
  441. right = Series(Categorical(["a", "b", np.nan], categories=["a", "b"]))
  442. tm.assert_series_equal(left, right)
  443. def test_constructor_maskedarray(self):
  444. data = ma.masked_all((3,), dtype=float)
  445. result = Series(data)
  446. expected = Series([np.nan, np.nan, np.nan])
  447. tm.assert_series_equal(result, expected)
  448. data[0] = 0.0
  449. data[2] = 2.0
  450. index = ["a", "b", "c"]
  451. result = Series(data, index=index)
  452. expected = Series([0.0, np.nan, 2.0], index=index)
  453. tm.assert_series_equal(result, expected)
  454. data[1] = 1.0
  455. result = Series(data, index=index)
  456. expected = Series([0.0, 1.0, 2.0], index=index)
  457. tm.assert_series_equal(result, expected)
  458. data = ma.masked_all((3,), dtype=int)
  459. result = Series(data)
  460. expected = Series([np.nan, np.nan, np.nan], dtype=float)
  461. tm.assert_series_equal(result, expected)
  462. data[0] = 0
  463. data[2] = 2
  464. index = ["a", "b", "c"]
  465. result = Series(data, index=index)
  466. expected = Series([0, np.nan, 2], index=index, dtype=float)
  467. tm.assert_series_equal(result, expected)
  468. data[1] = 1
  469. result = Series(data, index=index)
  470. expected = Series([0, 1, 2], index=index, dtype=int)
  471. with pytest.raises(AssertionError, match="Series classes are different"):
  472. # TODO should this be raising at all?
  473. # https://github.com/pandas-dev/pandas/issues/56131
  474. tm.assert_series_equal(result, expected)
  475. data = ma.masked_all((3,), dtype=bool)
  476. result = Series(data)
  477. expected = Series([np.nan, np.nan, np.nan], dtype=object)
  478. tm.assert_series_equal(result, expected)
  479. data[0] = True
  480. data[2] = False
  481. index = ["a", "b", "c"]
  482. result = Series(data, index=index)
  483. expected = Series([True, np.nan, False], index=index, dtype=object)
  484. tm.assert_series_equal(result, expected)
  485. data[1] = True
  486. result = Series(data, index=index)
  487. expected = Series([True, True, False], index=index, dtype=bool)
  488. with pytest.raises(AssertionError, match="Series classes are different"):
  489. # TODO should this be raising at all?
  490. # https://github.com/pandas-dev/pandas/issues/56131
  491. tm.assert_series_equal(result, expected)
  492. data = ma.masked_all((3,), dtype="M8[ns]")
  493. result = Series(data)
  494. expected = Series([iNaT, iNaT, iNaT], dtype="M8[ns]")
  495. tm.assert_series_equal(result, expected)
  496. data[0] = datetime(2001, 1, 1)
  497. data[2] = datetime(2001, 1, 3)
  498. index = ["a", "b", "c"]
  499. result = Series(data, index=index)
  500. expected = Series(
  501. [datetime(2001, 1, 1), iNaT, datetime(2001, 1, 3)],
  502. index=index,
  503. dtype="M8[ns]",
  504. )
  505. tm.assert_series_equal(result, expected)
  506. data[1] = datetime(2001, 1, 2)
  507. result = Series(data, index=index)
  508. expected = Series(
  509. [datetime(2001, 1, 1), datetime(2001, 1, 2), datetime(2001, 1, 3)],
  510. index=index,
  511. dtype="M8[ns]",
  512. )
  513. tm.assert_series_equal(result, expected)
  514. def test_constructor_maskedarray_hardened(self):
  515. # Check numpy masked arrays with hard masks -- from GH24574
  516. data = ma.masked_all((3,), dtype=float).harden_mask()
  517. result = Series(data)
  518. expected = Series([np.nan, np.nan, np.nan])
  519. tm.assert_series_equal(result, expected)
  520. def test_series_ctor_plus_datetimeindex(self, using_copy_on_write):
  521. rng = date_range("20090415", "20090519", freq="B")
  522. data = {k: 1 for k in rng}
  523. result = Series(data, index=rng)
  524. if using_copy_on_write:
  525. assert result.index.is_(rng)
  526. else:
  527. assert result.index is rng
  528. def test_constructor_default_index(self):
  529. s = Series([0, 1, 2])
  530. tm.assert_index_equal(s.index, Index(range(3)), exact=True)
  531. @pytest.mark.parametrize(
  532. "input",
  533. [
  534. [1, 2, 3],
  535. (1, 2, 3),
  536. list(range(3)),
  537. Categorical(["a", "b", "a"]),
  538. (i for i in range(3)),
  539. (x for x in range(3)),
  540. ],
  541. )
  542. def test_constructor_index_mismatch(self, input):
  543. # GH 19342
  544. # test that construction of a Series with an index of different length
  545. # raises an error
  546. msg = r"Length of values \(3\) does not match length of index \(4\)"
  547. with pytest.raises(ValueError, match=msg):
  548. Series(input, index=np.arange(4))
  549. def test_constructor_numpy_scalar(self):
  550. # GH 19342
  551. # construction with a numpy scalar
  552. # should not raise
  553. result = Series(np.array(100), index=np.arange(4), dtype="int64")
  554. expected = Series(100, index=np.arange(4), dtype="int64")
  555. tm.assert_series_equal(result, expected)
  556. def test_constructor_broadcast_list(self):
  557. # GH 19342
  558. # construction with single-element container and index
  559. # should raise
  560. msg = r"Length of values \(1\) does not match length of index \(3\)"
  561. with pytest.raises(ValueError, match=msg):
  562. Series(["foo"], index=["a", "b", "c"])
  563. def test_constructor_corner(self):
  564. df = DataFrame(range(5), index=date_range("2020-01-01", periods=5))
  565. objs = [df, df]
  566. s = Series(objs, index=[0, 1])
  567. assert isinstance(s, Series)
  568. def test_constructor_sanitize(self):
  569. s = Series(np.array([1.0, 1.0, 8.0]), dtype="i8")
  570. assert s.dtype == np.dtype("i8")
  571. msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
  572. with pytest.raises(IntCastingNaNError, match=msg):
  573. Series(np.array([1.0, 1.0, np.nan]), copy=True, dtype="i8")
  574. def test_constructor_copy(self):
  575. # GH15125
  576. # test dtype parameter has no side effects on copy=True
  577. for data in [[1.0], np.array([1.0])]:
  578. x = Series(data)
  579. y = Series(x, copy=True, dtype=float)
  580. # copy=True maintains original data in Series
  581. tm.assert_series_equal(x, y)
  582. # changes to origin of copy does not affect the copy
  583. x[0] = 2.0
  584. assert not x.equals(y)
  585. assert x[0] == 2.0
  586. assert y[0] == 1.0
  587. @td.skip_array_manager_invalid_test # TODO(ArrayManager) rewrite test
  588. @pytest.mark.parametrize(
  589. "index",
  590. [
  591. date_range("20170101", periods=3, tz="US/Eastern"),
  592. date_range("20170101", periods=3),
  593. timedelta_range("1 day", periods=3),
  594. period_range("2012Q1", periods=3, freq="Q"),
  595. Index(list("abc")),
  596. Index([1, 2, 3]),
  597. RangeIndex(0, 3),
  598. ],
  599. ids=lambda x: type(x).__name__,
  600. )
  601. def test_constructor_limit_copies(self, index):
  602. # GH 17449
  603. # limit copies of input
  604. s = Series(index)
  605. # we make 1 copy; this is just a smoke test here
  606. assert s._mgr.blocks[0].values is not index
  607. def test_constructor_shallow_copy(self):
  608. # constructing a Series from Series with copy=False should still
  609. # give a "shallow" copy (share data, not attributes)
  610. # https://github.com/pandas-dev/pandas/issues/49523
  611. s = Series([1, 2, 3])
  612. s_orig = s.copy()
  613. s2 = Series(s)
  614. assert s2._mgr is not s._mgr
  615. # Overwriting index of s2 doesn't change s
  616. s2.index = ["a", "b", "c"]
  617. tm.assert_series_equal(s, s_orig)
  618. def test_constructor_pass_none(self):
  619. s = Series(None, index=range(5))
  620. assert s.dtype == np.float64
  621. s = Series(None, index=range(5), dtype=object)
  622. assert s.dtype == np.object_
  623. # GH 7431
  624. # inference on the index
  625. s = Series(index=np.array([None]))
  626. expected = Series(index=Index([None]))
  627. tm.assert_series_equal(s, expected)
  628. def test_constructor_pass_nan_nat(self):
  629. # GH 13467
  630. exp = Series([np.nan, np.nan], dtype=np.float64)
  631. assert exp.dtype == np.float64
  632. tm.assert_series_equal(Series([np.nan, np.nan]), exp)
  633. tm.assert_series_equal(Series(np.array([np.nan, np.nan])), exp)
  634. exp = Series([NaT, NaT])
  635. assert exp.dtype == "datetime64[ns]"
  636. tm.assert_series_equal(Series([NaT, NaT]), exp)
  637. tm.assert_series_equal(Series(np.array([NaT, NaT])), exp)
  638. tm.assert_series_equal(Series([NaT, np.nan]), exp)
  639. tm.assert_series_equal(Series(np.array([NaT, np.nan])), exp)
  640. tm.assert_series_equal(Series([np.nan, NaT]), exp)
  641. tm.assert_series_equal(Series(np.array([np.nan, NaT])), exp)
  642. def test_constructor_cast(self):
  643. msg = "could not convert string to float"
  644. with pytest.raises(ValueError, match=msg):
  645. Series(["a", "b", "c"], dtype=float)
  646. def test_constructor_signed_int_overflow_raises(self):
  647. # GH#41734 disallow silent overflow, enforced in 2.0
  648. if np_version_gt2:
  649. msg = "The elements provided in the data cannot all be casted to the dtype"
  650. err = OverflowError
  651. else:
  652. msg = "Values are too large to be losslessly converted"
  653. err = ValueError
  654. with pytest.raises(err, match=msg):
  655. Series([1, 200, 923442], dtype="int8")
  656. with pytest.raises(err, match=msg):
  657. Series([1, 200, 923442], dtype="uint8")
  658. @pytest.mark.parametrize(
  659. "values",
  660. [
  661. np.array([1], dtype=np.uint16),
  662. np.array([1], dtype=np.uint32),
  663. np.array([1], dtype=np.uint64),
  664. [np.uint16(1)],
  665. [np.uint32(1)],
  666. [np.uint64(1)],
  667. ],
  668. )
  669. def test_constructor_numpy_uints(self, values):
  670. # GH#47294
  671. value = values[0]
  672. result = Series(values)
  673. assert result[0].dtype == value.dtype
  674. assert result[0] == value
  675. def test_constructor_unsigned_dtype_overflow(self, any_unsigned_int_numpy_dtype):
  676. # see gh-15832
  677. if np_version_gt2:
  678. msg = (
  679. f"The elements provided in the data cannot "
  680. f"all be casted to the dtype {any_unsigned_int_numpy_dtype}"
  681. )
  682. else:
  683. msg = "Trying to coerce negative values to unsigned integers"
  684. with pytest.raises(OverflowError, match=msg):
  685. Series([-1], dtype=any_unsigned_int_numpy_dtype)
  686. def test_constructor_floating_data_int_dtype(self, frame_or_series):
  687. # GH#40110
  688. arr = np.random.default_rng(2).standard_normal(2)
  689. # Long-standing behavior (for Series, new in 2.0 for DataFrame)
  690. # has been to ignore the dtype on these;
  691. # not clear if this is what we want long-term
  692. # expected = frame_or_series(arr)
  693. # GH#49599 as of 2.0 we raise instead of silently retaining float dtype
  694. msg = "Trying to coerce float values to integer"
  695. with pytest.raises(ValueError, match=msg):
  696. frame_or_series(arr, dtype="i8")
  697. with pytest.raises(ValueError, match=msg):
  698. frame_or_series(list(arr), dtype="i8")
  699. # pre-2.0, when we had NaNs, we silently ignored the integer dtype
  700. arr[0] = np.nan
  701. # expected = frame_or_series(arr)
  702. msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
  703. with pytest.raises(IntCastingNaNError, match=msg):
  704. frame_or_series(arr, dtype="i8")
  705. exc = IntCastingNaNError
  706. if frame_or_series is Series:
  707. # TODO: try to align these
  708. exc = ValueError
  709. msg = "cannot convert float NaN to integer"
  710. with pytest.raises(exc, match=msg):
  711. # same behavior if we pass list instead of the ndarray
  712. frame_or_series(list(arr), dtype="i8")
  713. # float array that can be losslessly cast to integers
  714. arr = np.array([1.0, 2.0], dtype="float64")
  715. expected = frame_or_series(arr.astype("i8"))
  716. obj = frame_or_series(arr, dtype="i8")
  717. tm.assert_equal(obj, expected)
  718. obj = frame_or_series(list(arr), dtype="i8")
  719. tm.assert_equal(obj, expected)
  720. def test_constructor_coerce_float_fail(self, any_int_numpy_dtype):
  721. # see gh-15832
  722. # Updated: make sure we treat this list the same as we would treat
  723. # the equivalent ndarray
  724. # GH#49599 pre-2.0 we silently retained float dtype, in 2.0 we raise
  725. vals = [1, 2, 3.5]
  726. msg = "Trying to coerce float values to integer"
  727. with pytest.raises(ValueError, match=msg):
  728. Series(vals, dtype=any_int_numpy_dtype)
  729. with pytest.raises(ValueError, match=msg):
  730. Series(np.array(vals), dtype=any_int_numpy_dtype)
  731. def test_constructor_coerce_float_valid(self, float_numpy_dtype):
  732. s = Series([1, 2, 3.5], dtype=float_numpy_dtype)
  733. expected = Series([1, 2, 3.5]).astype(float_numpy_dtype)
  734. tm.assert_series_equal(s, expected)
  735. def test_constructor_invalid_coerce_ints_with_float_nan(self, any_int_numpy_dtype):
  736. # GH 22585
  737. # Updated: make sure we treat this list the same as we would treat the
  738. # equivalent ndarray
  739. vals = [1, 2, np.nan]
  740. # pre-2.0 this would return with a float dtype, in 2.0 we raise
  741. msg = "cannot convert float NaN to integer"
  742. with pytest.raises(ValueError, match=msg):
  743. Series(vals, dtype=any_int_numpy_dtype)
  744. msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
  745. with pytest.raises(IntCastingNaNError, match=msg):
  746. Series(np.array(vals), dtype=any_int_numpy_dtype)
  747. def test_constructor_dtype_no_cast(self, using_copy_on_write, warn_copy_on_write):
  748. # see gh-1572
  749. s = Series([1, 2, 3])
  750. s2 = Series(s, dtype=np.int64)
  751. warn = FutureWarning if warn_copy_on_write else None
  752. with tm.assert_produces_warning(warn):
  753. s2[1] = 5
  754. if using_copy_on_write:
  755. assert s[1] == 2
  756. else:
  757. assert s[1] == 5
  758. def test_constructor_datelike_coercion(self):
  759. # GH 9477
  760. # incorrectly inferring on dateimelike looking when object dtype is
  761. # specified
  762. s = Series([Timestamp("20130101"), "NOV"], dtype=object)
  763. assert s.iloc[0] == Timestamp("20130101")
  764. assert s.iloc[1] == "NOV"
  765. assert s.dtype == object
  766. def test_constructor_datelike_coercion2(self):
  767. # the dtype was being reset on the slicing and re-inferred to datetime
  768. # even thought the blocks are mixed
  769. belly = "216 3T19".split()
  770. wing1 = "2T15 4H19".split()
  771. wing2 = "416 4T20".split()
  772. mat = pd.to_datetime("2016-01-22 2019-09-07".split())
  773. df = DataFrame({"wing1": wing1, "wing2": wing2, "mat": mat}, index=belly)
  774. result = df.loc["3T19"]
  775. assert result.dtype == object
  776. result = df.loc["216"]
  777. assert result.dtype == object
  778. def test_constructor_mixed_int_and_timestamp(self, frame_or_series):
  779. # specifically Timestamp with nanos, not datetimes
  780. objs = [Timestamp(9), 10, NaT._value]
  781. result = frame_or_series(objs, dtype="M8[ns]")
  782. expected = frame_or_series([Timestamp(9), Timestamp(10), NaT])
  783. tm.assert_equal(result, expected)
  784. def test_constructor_datetimes_with_nulls(self):
  785. # gh-15869
  786. for arr in [
  787. np.array([None, None, None, None, datetime.now(), None]),
  788. np.array([None, None, datetime.now(), None]),
  789. ]:
  790. result = Series(arr)
  791. assert result.dtype == "M8[ns]"
  792. def test_constructor_dtype_datetime64(self):
  793. s = Series(iNaT, dtype="M8[ns]", index=range(5))
  794. assert isna(s).all()
  795. # in theory this should be all nulls, but since
  796. # we are not specifying a dtype is ambiguous
  797. s = Series(iNaT, index=range(5))
  798. assert not isna(s).all()
  799. s = Series(np.nan, dtype="M8[ns]", index=range(5))
  800. assert isna(s).all()
  801. s = Series([datetime(2001, 1, 2, 0, 0), iNaT], dtype="M8[ns]")
  802. assert isna(s[1])
  803. assert s.dtype == "M8[ns]"
  804. s = Series([datetime(2001, 1, 2, 0, 0), np.nan], dtype="M8[ns]")
  805. assert isna(s[1])
  806. assert s.dtype == "M8[ns]"
  807. def test_constructor_dtype_datetime64_10(self):
  808. # GH3416
  809. pydates = [datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3)]
  810. dates = [np.datetime64(x) for x in pydates]
  811. ser = Series(dates)
  812. assert ser.dtype == "M8[ns]"
  813. ser.iloc[0] = np.nan
  814. assert ser.dtype == "M8[ns]"
  815. # GH3414 related
  816. expected = Series(pydates, dtype="datetime64[ms]")
  817. result = Series(Series(dates).astype(np.int64) / 1000000, dtype="M8[ms]")
  818. tm.assert_series_equal(result, expected)
  819. result = Series(dates, dtype="datetime64[ms]")
  820. tm.assert_series_equal(result, expected)
  821. expected = Series(
  822. [NaT, datetime(2013, 1, 2), datetime(2013, 1, 3)], dtype="datetime64[ns]"
  823. )
  824. result = Series([np.nan] + dates[1:], dtype="datetime64[ns]")
  825. tm.assert_series_equal(result, expected)
  826. def test_constructor_dtype_datetime64_11(self):
  827. pydates = [datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3)]
  828. dates = [np.datetime64(x) for x in pydates]
  829. dts = Series(dates, dtype="datetime64[ns]")
  830. # valid astype
  831. dts.astype("int64")
  832. # invalid casting
  833. msg = r"Converting from datetime64\[ns\] to int32 is not supported"
  834. with pytest.raises(TypeError, match=msg):
  835. dts.astype("int32")
  836. # ints are ok
  837. # we test with np.int64 to get similar results on
  838. # windows / 32-bit platforms
  839. result = Series(dts, dtype=np.int64)
  840. expected = Series(dts.astype(np.int64))
  841. tm.assert_series_equal(result, expected)
  842. def test_constructor_dtype_datetime64_9(self):
  843. # invalid dates can be help as object
  844. result = Series([datetime(2, 1, 1)])
  845. assert result[0] == datetime(2, 1, 1, 0, 0)
  846. result = Series([datetime(3000, 1, 1)])
  847. assert result[0] == datetime(3000, 1, 1, 0, 0)
  848. def test_constructor_dtype_datetime64_8(self):
  849. # don't mix types
  850. result = Series([Timestamp("20130101"), 1], index=["a", "b"])
  851. assert result["a"] == Timestamp("20130101")
  852. assert result["b"] == 1
  853. def test_constructor_dtype_datetime64_7(self):
  854. # GH6529
  855. # coerce datetime64 non-ns properly
  856. dates = date_range("01-Jan-2015", "01-Dec-2015", freq="ME")
  857. values2 = dates.view(np.ndarray).astype("datetime64[ns]")
  858. expected = Series(values2, index=dates)
  859. for unit in ["s", "D", "ms", "us", "ns"]:
  860. dtype = np.dtype(f"M8[{unit}]")
  861. values1 = dates.view(np.ndarray).astype(dtype)
  862. result = Series(values1, dates)
  863. if unit == "D":
  864. # for unit="D" we cast to nearest-supported reso, i.e. "s"
  865. dtype = np.dtype("M8[s]")
  866. assert result.dtype == dtype
  867. tm.assert_series_equal(result, expected.astype(dtype))
  868. # GH 13876
  869. # coerce to non-ns to object properly
  870. expected = Series(values2, index=dates, dtype=object)
  871. for dtype in ["s", "D", "ms", "us", "ns"]:
  872. values1 = dates.view(np.ndarray).astype(f"M8[{dtype}]")
  873. result = Series(values1, index=dates, dtype=object)
  874. tm.assert_series_equal(result, expected)
  875. # leave datetime.date alone
  876. dates2 = np.array([d.date() for d in dates.to_pydatetime()], dtype=object)
  877. series1 = Series(dates2, dates)
  878. tm.assert_numpy_array_equal(series1.values, dates2)
  879. assert series1.dtype == object
  880. def test_constructor_dtype_datetime64_6(self):
  881. # as of 2.0, these no longer infer datetime64 based on the strings,
  882. # matching the Index behavior
  883. ser = Series([None, NaT, "2013-08-05 15:30:00.000001"])
  884. assert ser.dtype == object
  885. ser = Series([np.nan, NaT, "2013-08-05 15:30:00.000001"])
  886. assert ser.dtype == object
  887. ser = Series([NaT, None, "2013-08-05 15:30:00.000001"])
  888. assert ser.dtype == object
  889. ser = Series([NaT, np.nan, "2013-08-05 15:30:00.000001"])
  890. assert ser.dtype == object
  891. def test_constructor_dtype_datetime64_5(self):
  892. # tz-aware (UTC and other tz's)
  893. # GH 8411
  894. dr = date_range("20130101", periods=3)
  895. assert Series(dr).iloc[0].tz is None
  896. dr = date_range("20130101", periods=3, tz="UTC")
  897. assert str(Series(dr).iloc[0].tz) == "UTC"
  898. dr = date_range("20130101", periods=3, tz="US/Eastern")
  899. assert str(Series(dr).iloc[0].tz) == "US/Eastern"
  900. def test_constructor_dtype_datetime64_4(self):
  901. # non-convertible
  902. ser = Series([1479596223000, -1479590, NaT])
  903. assert ser.dtype == "object"
  904. assert ser[2] is NaT
  905. assert "NaT" in str(ser)
  906. def test_constructor_dtype_datetime64_3(self):
  907. # if we passed a NaT it remains
  908. ser = Series([datetime(2010, 1, 1), datetime(2, 1, 1), NaT])
  909. assert ser.dtype == "object"
  910. assert ser[2] is NaT
  911. assert "NaT" in str(ser)
  912. def test_constructor_dtype_datetime64_2(self):
  913. # if we passed a nan it remains
  914. ser = Series([datetime(2010, 1, 1), datetime(2, 1, 1), np.nan])
  915. assert ser.dtype == "object"
  916. assert ser[2] is np.nan
  917. assert "NaN" in str(ser)
  918. def test_constructor_with_datetime_tz(self):
  919. # 8260
  920. # support datetime64 with tz
  921. dr = date_range("20130101", periods=3, tz="US/Eastern")
  922. s = Series(dr)
  923. assert s.dtype.name == "datetime64[ns, US/Eastern]"
  924. assert s.dtype == "datetime64[ns, US/Eastern]"
  925. assert isinstance(s.dtype, DatetimeTZDtype)
  926. assert "datetime64[ns, US/Eastern]" in str(s)
  927. # export
  928. result = s.values
  929. assert isinstance(result, np.ndarray)
  930. assert result.dtype == "datetime64[ns]"
  931. exp = DatetimeIndex(result)
  932. exp = exp.tz_localize("UTC").tz_convert(tz=s.dt.tz)
  933. tm.assert_index_equal(dr, exp)
  934. # indexing
  935. result = s.iloc[0]
  936. assert result == Timestamp("2013-01-01 00:00:00-0500", tz="US/Eastern")
  937. result = s[0]
  938. assert result == Timestamp("2013-01-01 00:00:00-0500", tz="US/Eastern")
  939. result = s[Series([True, True, False], index=s.index)]
  940. tm.assert_series_equal(result, s[0:2])
  941. result = s.iloc[0:1]
  942. tm.assert_series_equal(result, Series(dr[0:1]))
  943. # concat
  944. result = pd.concat([s.iloc[0:1], s.iloc[1:]])
  945. tm.assert_series_equal(result, s)
  946. # short str
  947. assert "datetime64[ns, US/Eastern]" in str(s)
  948. # formatting with NaT
  949. result = s.shift()
  950. assert "datetime64[ns, US/Eastern]" in str(result)
  951. assert "NaT" in str(result)
  952. result = DatetimeIndex(s, freq="infer")
  953. tm.assert_index_equal(result, dr)
  954. def test_constructor_with_datetime_tz5(self):
  955. # long str
  956. ser = Series(date_range("20130101", periods=1000, tz="US/Eastern"))
  957. assert "datetime64[ns, US/Eastern]" in str(ser)
  958. def test_constructor_with_datetime_tz4(self):
  959. # inference
  960. ser = Series(
  961. [
  962. Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"),
  963. Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"),
  964. ]
  965. )
  966. assert ser.dtype == "datetime64[ns, US/Pacific]"
  967. assert lib.infer_dtype(ser, skipna=True) == "datetime64"
  968. def test_constructor_with_datetime_tz3(self):
  969. ser = Series(
  970. [
  971. Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"),
  972. Timestamp("2013-01-02 14:00:00-0800", tz="US/Eastern"),
  973. ]
  974. )
  975. assert ser.dtype == "object"
  976. assert lib.infer_dtype(ser, skipna=True) == "datetime"
  977. def test_constructor_with_datetime_tz2(self):
  978. # with all NaT
  979. ser = Series(NaT, index=[0, 1], dtype="datetime64[ns, US/Eastern]")
  980. dti = DatetimeIndex(["NaT", "NaT"], tz="US/Eastern").as_unit("ns")
  981. expected = Series(dti)
  982. tm.assert_series_equal(ser, expected)
  983. def test_constructor_no_partial_datetime_casting(self):
  984. # GH#40111
  985. vals = [
  986. "nan",
  987. Timestamp("1990-01-01"),
  988. "2015-03-14T16:15:14.123-08:00",
  989. "2019-03-04T21:56:32.620-07:00",
  990. None,
  991. ]
  992. ser = Series(vals)
  993. assert all(ser[i] is vals[i] for i in range(len(vals)))
  994. @pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
  995. @pytest.mark.parametrize("kind", ["M", "m"])
  996. @pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])
  997. def test_construction_to_datetimelike_unit(self, arr_dtype, kind, unit):
  998. # tests all units
  999. # gh-19223
  1000. # TODO: GH#19223 was about .astype, doesn't belong here
  1001. dtype = f"{kind}8[{unit}]"
  1002. arr = np.array([1, 2, 3], dtype=arr_dtype)
  1003. ser = Series(arr)
  1004. result = ser.astype(dtype)
  1005. expected = Series(arr.astype(dtype))
  1006. if unit in ["ns", "us", "ms", "s"]:
  1007. assert result.dtype == dtype
  1008. assert expected.dtype == dtype
  1009. else:
  1010. # Otherwise we cast to nearest-supported unit, i.e. seconds
  1011. assert result.dtype == f"{kind}8[s]"
  1012. assert expected.dtype == f"{kind}8[s]"
  1013. tm.assert_series_equal(result, expected)
  1014. @pytest.mark.parametrize("arg", ["2013-01-01 00:00:00", NaT, np.nan, None])
  1015. def test_constructor_with_naive_string_and_datetimetz_dtype(self, arg):
  1016. # GH 17415: With naive string
  1017. result = Series([arg], dtype="datetime64[ns, CET]")
  1018. expected = Series(Timestamp(arg)).dt.tz_localize("CET")
  1019. tm.assert_series_equal(result, expected)
  1020. def test_constructor_datetime64_bigendian(self):
  1021. # GH#30976
  1022. ms = np.datetime64(1, "ms")
  1023. arr = np.array([np.datetime64(1, "ms")], dtype=">M8[ms]")
  1024. result = Series(arr)
  1025. expected = Series([Timestamp(ms)]).astype("M8[ms]")
  1026. assert expected.dtype == "M8[ms]"
  1027. tm.assert_series_equal(result, expected)
  1028. @pytest.mark.parametrize("interval_constructor", [IntervalIndex, IntervalArray])
  1029. def test_construction_interval(self, interval_constructor):
  1030. # construction from interval & array of intervals
  1031. intervals = interval_constructor.from_breaks(np.arange(3), closed="right")
  1032. result = Series(intervals)
  1033. assert result.dtype == "interval[int64, right]"
  1034. tm.assert_index_equal(Index(result.values), Index(intervals))
  1035. @pytest.mark.parametrize(
  1036. "data_constructor", [list, np.array], ids=["list", "ndarray[object]"]
  1037. )
  1038. def test_constructor_infer_interval(self, data_constructor):
  1039. # GH 23563: consistent closed results in interval dtype
  1040. data = [Interval(0, 1), Interval(0, 2), None]
  1041. result = Series(data_constructor(data))
  1042. expected = Series(IntervalArray(data))
  1043. assert result.dtype == "interval[float64, right]"
  1044. tm.assert_series_equal(result, expected)
  1045. @pytest.mark.parametrize(
  1046. "data_constructor", [list, np.array], ids=["list", "ndarray[object]"]
  1047. )
  1048. def test_constructor_interval_mixed_closed(self, data_constructor):
  1049. # GH 23563: mixed closed results in object dtype (not interval dtype)
  1050. data = [Interval(0, 1, closed="both"), Interval(0, 2, closed="neither")]
  1051. result = Series(data_constructor(data))
  1052. assert result.dtype == object
  1053. assert result.tolist() == data
  1054. def test_construction_consistency(self):
  1055. # make sure that we are not re-localizing upon construction
  1056. # GH 14928
  1057. ser = Series(date_range("20130101", periods=3, tz="US/Eastern"))
  1058. result = Series(ser, dtype=ser.dtype)
  1059. tm.assert_series_equal(result, ser)
  1060. result = Series(ser.dt.tz_convert("UTC"), dtype=ser.dtype)
  1061. tm.assert_series_equal(result, ser)
  1062. # Pre-2.0 dt64 values were treated as utc, which was inconsistent
  1063. # with DatetimeIndex, which treats them as wall times, see GH#33401
  1064. result = Series(ser.values, dtype=ser.dtype)
  1065. expected = Series(ser.values).dt.tz_localize(ser.dtype.tz)
  1066. tm.assert_series_equal(result, expected)
  1067. with tm.assert_produces_warning(None):
  1068. # one suggested alternative to the deprecated (changed in 2.0) usage
  1069. middle = Series(ser.values).dt.tz_localize("UTC")
  1070. result = middle.dt.tz_convert(ser.dtype.tz)
  1071. tm.assert_series_equal(result, ser)
  1072. with tm.assert_produces_warning(None):
  1073. # the other suggested alternative to the deprecated usage
  1074. result = Series(ser.values.view("int64"), dtype=ser.dtype)
  1075. tm.assert_series_equal(result, ser)
  1076. @pytest.mark.parametrize(
  1077. "data_constructor", [list, np.array], ids=["list", "ndarray[object]"]
  1078. )
  1079. def test_constructor_infer_period(self, data_constructor):
  1080. data = [Period("2000", "D"), Period("2001", "D"), None]
  1081. result = Series(data_constructor(data))
  1082. expected = Series(period_array(data))
  1083. tm.assert_series_equal(result, expected)
  1084. assert result.dtype == "Period[D]"
  1085. @pytest.mark.xfail(reason="PeriodDtype Series not supported yet")
  1086. def test_construct_from_ints_including_iNaT_scalar_period_dtype(self):
  1087. series = Series([0, 1000, 2000, pd._libs.iNaT], dtype="period[D]")
  1088. val = series[3]
  1089. assert isna(val)
  1090. series[2] = val
  1091. assert isna(series[2])
  1092. def test_constructor_period_incompatible_frequency(self):
  1093. data = [Period("2000", "D"), Period("2001", "Y")]
  1094. result = Series(data)
  1095. assert result.dtype == object
  1096. assert result.tolist() == data
  1097. def test_constructor_periodindex(self):
  1098. # GH7932
  1099. # converting a PeriodIndex when put in a Series
  1100. pi = period_range("20130101", periods=5, freq="D")
  1101. s = Series(pi)
  1102. assert s.dtype == "Period[D]"
  1103. with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
  1104. expected = Series(pi.astype(object))
  1105. tm.assert_series_equal(s, expected)
  1106. def test_constructor_dict(self):
  1107. d = {"a": 0.0, "b": 1.0, "c": 2.0}
  1108. result = Series(d)
  1109. expected = Series(d, index=sorted(d.keys()))
  1110. tm.assert_series_equal(result, expected)
  1111. result = Series(d, index=["b", "c", "d", "a"])
  1112. expected = Series([1, 2, np.nan, 0], index=["b", "c", "d", "a"])
  1113. tm.assert_series_equal(result, expected)
  1114. pidx = period_range("2020-01-01", periods=10, freq="D")
  1115. d = {pidx[0]: 0, pidx[1]: 1}
  1116. result = Series(d, index=pidx)
  1117. expected = Series(np.nan, pidx, dtype=np.float64)
  1118. expected.iloc[0] = 0
  1119. expected.iloc[1] = 1
  1120. tm.assert_series_equal(result, expected)
  1121. def test_constructor_dict_list_value_explicit_dtype(self):
  1122. # GH 18625
  1123. d = {"a": [[2], [3], [4]]}
  1124. result = Series(d, index=["a"], dtype="object")
  1125. expected = Series(d, index=["a"])
  1126. tm.assert_series_equal(result, expected)
  1127. def test_constructor_dict_order(self):
  1128. # GH19018
  1129. # initialization ordering: by insertion order
  1130. d = {"b": 1, "a": 0, "c": 2}
  1131. result = Series(d)
  1132. expected = Series([1, 0, 2], index=list("bac"))
  1133. tm.assert_series_equal(result, expected)
  1134. def test_constructor_dict_extension(self, ea_scalar_and_dtype, request):
  1135. ea_scalar, ea_dtype = ea_scalar_and_dtype
  1136. if isinstance(ea_scalar, Timestamp):
  1137. mark = pytest.mark.xfail(
  1138. reason="Construction from dict goes through "
  1139. "maybe_convert_objects which casts to nano"
  1140. )
  1141. request.applymarker(mark)
  1142. d = {"a": ea_scalar}
  1143. result = Series(d, index=["a"])
  1144. expected = Series(ea_scalar, index=["a"], dtype=ea_dtype)
  1145. assert result.dtype == ea_dtype
  1146. tm.assert_series_equal(result, expected)
  1147. @pytest.mark.parametrize("value", [2, np.nan, None, float("nan")])
  1148. def test_constructor_dict_nan_key(self, value):
  1149. # GH 18480
  1150. d = {1: "a", value: "b", float("nan"): "c", 4: "d"}
  1151. result = Series(d).sort_values()
  1152. expected = Series(["a", "b", "c", "d"], index=[1, value, np.nan, 4])
  1153. tm.assert_series_equal(result, expected)
  1154. # MultiIndex:
  1155. d = {(1, 1): "a", (2, np.nan): "b", (3, value): "c"}
  1156. result = Series(d).sort_values()
  1157. expected = Series(
  1158. ["a", "b", "c"], index=Index([(1, 1), (2, np.nan), (3, value)])
  1159. )
  1160. tm.assert_series_equal(result, expected)
  1161. def test_constructor_dict_datetime64_index(self):
  1162. # GH 9456
  1163. dates_as_str = ["1984-02-19", "1988-11-06", "1989-12-03", "1990-03-15"]
  1164. values = [42544017.198965244, 1234565, 40512335.181958228, -1]
  1165. def create_data(constructor):
  1166. return dict(zip((constructor(x) for x in dates_as_str), values))
  1167. data_datetime64 = create_data(np.datetime64)
  1168. data_datetime = create_data(lambda x: datetime.strptime(x, "%Y-%m-%d"))
  1169. data_Timestamp = create_data(Timestamp)
  1170. expected = Series(values, (Timestamp(x) for x in dates_as_str))
  1171. result_datetime64 = Series(data_datetime64)
  1172. result_datetime = Series(data_datetime)
  1173. result_Timestamp = Series(data_Timestamp)
  1174. tm.assert_series_equal(result_datetime64, expected)
  1175. tm.assert_series_equal(result_datetime, expected)
  1176. tm.assert_series_equal(result_Timestamp, expected)
  1177. def test_constructor_dict_tuple_indexer(self):
  1178. # GH 12948
  1179. data = {(1, 1, None): -1.0}
  1180. result = Series(data)
  1181. expected = Series(
  1182. -1.0, index=MultiIndex(levels=[[1], [1], [np.nan]], codes=[[0], [0], [-1]])
  1183. )
  1184. tm.assert_series_equal(result, expected)
  1185. def test_constructor_mapping(self, non_dict_mapping_subclass):
  1186. # GH 29788
  1187. ndm = non_dict_mapping_subclass({3: "three"})
  1188. result = Series(ndm)
  1189. expected = Series(["three"], index=[3])
  1190. tm.assert_series_equal(result, expected)
  1191. def test_constructor_list_of_tuples(self):
  1192. data = [(1, 1), (2, 2), (2, 3)]
  1193. s = Series(data)
  1194. assert list(s) == data
  1195. def test_constructor_tuple_of_tuples(self):
  1196. data = ((1, 1), (2, 2), (2, 3))
  1197. s = Series(data)
  1198. assert tuple(s) == data
  1199. def test_constructor_dict_of_tuples(self):
  1200. data = {(1, 2): 3, (None, 5): 6}
  1201. result = Series(data).sort_values()
  1202. expected = Series([3, 6], index=MultiIndex.from_tuples([(1, 2), (None, 5)]))
  1203. tm.assert_series_equal(result, expected)
  1204. # https://github.com/pandas-dev/pandas/issues/22698
  1205. @pytest.mark.filterwarnings("ignore:elementwise comparison:FutureWarning")
  1206. def test_fromDict(self, using_infer_string):
  1207. data = {"a": 0, "b": 1, "c": 2, "d": 3}
  1208. series = Series(data)
  1209. tm.assert_is_sorted(series.index)
  1210. data = {"a": 0, "b": "1", "c": "2", "d": datetime.now()}
  1211. series = Series(data)
  1212. assert series.dtype == np.object_
  1213. data = {"a": 0, "b": "1", "c": "2", "d": "3"}
  1214. series = Series(data)
  1215. assert series.dtype == np.object_ if not using_infer_string else "str"
  1216. data = {"a": "0", "b": "1"}
  1217. series = Series(data, dtype=float)
  1218. assert series.dtype == np.float64
  1219. def test_fromValue(self, datetime_series, using_infer_string):
  1220. nans = Series(np.nan, index=datetime_series.index, dtype=np.float64)
  1221. assert nans.dtype == np.float64
  1222. assert len(nans) == len(datetime_series)
  1223. strings = Series("foo", index=datetime_series.index)
  1224. assert strings.dtype == np.object_ if not using_infer_string else "str"
  1225. assert len(strings) == len(datetime_series)
  1226. d = datetime.now()
  1227. dates = Series(d, index=datetime_series.index)
  1228. assert dates.dtype == "M8[us]"
  1229. assert len(dates) == len(datetime_series)
  1230. # GH12336
  1231. # Test construction of categorical series from value
  1232. categorical = Series(0, index=datetime_series.index, dtype="category")
  1233. expected = Series(0, index=datetime_series.index).astype("category")
  1234. assert categorical.dtype == "category"
  1235. assert len(categorical) == len(datetime_series)
  1236. tm.assert_series_equal(categorical, expected)
  1237. def test_constructor_dtype_timedelta64(self):
  1238. # basic
  1239. td = Series([timedelta(days=i) for i in range(3)])
  1240. assert td.dtype == "timedelta64[ns]"
  1241. td = Series([timedelta(days=1)])
  1242. assert td.dtype == "timedelta64[ns]"
  1243. td = Series([timedelta(days=1), timedelta(days=2), np.timedelta64(1, "s")])
  1244. assert td.dtype == "timedelta64[ns]"
  1245. # mixed with NaT
  1246. td = Series([timedelta(days=1), NaT], dtype="m8[ns]")
  1247. assert td.dtype == "timedelta64[ns]"
  1248. td = Series([timedelta(days=1), np.nan], dtype="m8[ns]")
  1249. assert td.dtype == "timedelta64[ns]"
  1250. td = Series([np.timedelta64(300000000), NaT], dtype="m8[ns]")
  1251. assert td.dtype == "timedelta64[ns]"
  1252. # improved inference
  1253. # GH5689
  1254. td = Series([np.timedelta64(300000000), NaT])
  1255. assert td.dtype == "timedelta64[ns]"
  1256. # because iNaT is int, not coerced to timedelta
  1257. td = Series([np.timedelta64(300000000), iNaT])
  1258. assert td.dtype == "object"
  1259. td = Series([np.timedelta64(300000000), np.nan])
  1260. assert td.dtype == "timedelta64[ns]"
  1261. td = Series([NaT, np.timedelta64(300000000)])
  1262. assert td.dtype == "timedelta64[ns]"
  1263. td = Series([np.timedelta64(1, "s")])
  1264. assert td.dtype == "timedelta64[ns]"
  1265. # valid astype
  1266. td.astype("int64")
  1267. # invalid casting
  1268. msg = r"Converting from timedelta64\[ns\] to int32 is not supported"
  1269. with pytest.raises(TypeError, match=msg):
  1270. td.astype("int32")
  1271. # this is an invalid casting
  1272. msg = "|".join(
  1273. [
  1274. "Could not convert object to NumPy timedelta",
  1275. "Could not convert 'foo' to NumPy timedelta",
  1276. ]
  1277. )
  1278. with pytest.raises(ValueError, match=msg):
  1279. Series([timedelta(days=1), "foo"], dtype="m8[ns]")
  1280. # leave as object here
  1281. td = Series([timedelta(days=i) for i in range(3)] + ["foo"])
  1282. assert td.dtype == "object"
  1283. # as of 2.0, these no longer infer timedelta64 based on the strings,
  1284. # matching Index behavior
  1285. ser = Series([None, NaT, "1 Day"])
  1286. assert ser.dtype == object
  1287. ser = Series([np.nan, NaT, "1 Day"])
  1288. assert ser.dtype == object
  1289. ser = Series([NaT, None, "1 Day"])
  1290. assert ser.dtype == object
  1291. ser = Series([NaT, np.nan, "1 Day"])
  1292. assert ser.dtype == object
  1293. # GH 16406
  1294. def test_constructor_mixed_tz(self):
  1295. s = Series([Timestamp("20130101"), Timestamp("20130101", tz="US/Eastern")])
  1296. expected = Series(
  1297. [Timestamp("20130101"), Timestamp("20130101", tz="US/Eastern")],
  1298. dtype="object",
  1299. )
  1300. tm.assert_series_equal(s, expected)
  1301. def test_NaT_scalar(self):
  1302. series = Series([0, 1000, 2000, iNaT], dtype="M8[ns]")
  1303. val = series[3]
  1304. assert isna(val)
  1305. series[2] = val
  1306. assert isna(series[2])
  1307. def test_NaT_cast(self):
  1308. # GH10747
  1309. result = Series([np.nan]).astype("M8[ns]")
  1310. expected = Series([NaT], dtype="M8[ns]")
  1311. tm.assert_series_equal(result, expected)
  1312. def test_constructor_name_hashable(self):
  1313. for n in [777, 777.0, "name", datetime(2001, 11, 11), (1,), "\u05D0"]:
  1314. for data in [[1, 2, 3], np.ones(3), {"a": 0, "b": 1}]:
  1315. s = Series(data, name=n)
  1316. assert s.name == n
  1317. def test_constructor_name_unhashable(self):
  1318. msg = r"Series\.name must be a hashable type"
  1319. for n in [["name_list"], np.ones(2), {1: 2}]:
  1320. for data in [["name_list"], np.ones(2), {1: 2}]:
  1321. with pytest.raises(TypeError, match=msg):
  1322. Series(data, name=n)
  1323. def test_auto_conversion(self):
  1324. series = Series(list(date_range("1/1/2000", periods=10)))
  1325. assert series.dtype == "M8[ns]"
  1326. def test_convert_non_ns(self):
  1327. # convert from a numpy array of non-ns timedelta64
  1328. arr = np.array([1, 2, 3], dtype="timedelta64[s]")
  1329. ser = Series(arr)
  1330. assert ser.dtype == arr.dtype
  1331. tdi = timedelta_range("00:00:01", periods=3, freq="s").as_unit("s")
  1332. expected = Series(tdi)
  1333. assert expected.dtype == arr.dtype
  1334. tm.assert_series_equal(ser, expected)
  1335. # convert from a numpy array of non-ns datetime64
  1336. arr = np.array(
  1337. ["2013-01-01", "2013-01-02", "2013-01-03"], dtype="datetime64[D]"
  1338. )
  1339. ser = Series(arr)
  1340. expected = Series(date_range("20130101", periods=3, freq="D"), dtype="M8[s]")
  1341. assert expected.dtype == "M8[s]"
  1342. tm.assert_series_equal(ser, expected)
  1343. arr = np.array(
  1344. ["2013-01-01 00:00:01", "2013-01-01 00:00:02", "2013-01-01 00:00:03"],
  1345. dtype="datetime64[s]",
  1346. )
  1347. ser = Series(arr)
  1348. expected = Series(
  1349. date_range("20130101 00:00:01", periods=3, freq="s"), dtype="M8[s]"
  1350. )
  1351. assert expected.dtype == "M8[s]"
  1352. tm.assert_series_equal(ser, expected)
  1353. @pytest.mark.parametrize(
  1354. "index",
  1355. [
  1356. date_range("1/1/2000", periods=10),
  1357. timedelta_range("1 day", periods=10),
  1358. period_range("2000-Q1", periods=10, freq="Q"),
  1359. ],
  1360. ids=lambda x: type(x).__name__,
  1361. )
  1362. def test_constructor_cant_cast_datetimelike(self, index):
  1363. # floats are not ok
  1364. # strip Index to convert PeriodIndex -> Period
  1365. # We don't care whether the error message says
  1366. # PeriodIndex or PeriodArray
  1367. msg = f"Cannot cast {type(index).__name__.rstrip('Index')}.*? to "
  1368. with pytest.raises(TypeError, match=msg):
  1369. Series(index, dtype=float)
  1370. # ints are ok
  1371. # we test with np.int64 to get similar results on
  1372. # windows / 32-bit platforms
  1373. result = Series(index, dtype=np.int64)
  1374. expected = Series(index.astype(np.int64))
  1375. tm.assert_series_equal(result, expected)
  1376. @pytest.mark.parametrize(
  1377. "index",
  1378. [
  1379. date_range("1/1/2000", periods=10),
  1380. timedelta_range("1 day", periods=10),
  1381. period_range("2000-Q1", periods=10, freq="Q"),
  1382. ],
  1383. ids=lambda x: type(x).__name__,
  1384. )
  1385. def test_constructor_cast_object(self, index):
  1386. s = Series(index, dtype=object)
  1387. exp = Series(index).astype(object)
  1388. tm.assert_series_equal(s, exp)
  1389. s = Series(Index(index, dtype=object), dtype=object)
  1390. exp = Series(index).astype(object)
  1391. tm.assert_series_equal(s, exp)
  1392. s = Series(index.astype(object), dtype=object)
  1393. exp = Series(index).astype(object)
  1394. tm.assert_series_equal(s, exp)
  1395. @pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64])
  1396. def test_constructor_generic_timestamp_no_frequency(self, dtype, request):
  1397. # see gh-15524, gh-15987
  1398. msg = "dtype has no unit. Please pass in"
  1399. if np.dtype(dtype).name not in ["timedelta64", "datetime64"]:
  1400. mark = pytest.mark.xfail(reason="GH#33890 Is assigned ns unit")
  1401. request.applymarker(mark)
  1402. with pytest.raises(ValueError, match=msg):
  1403. Series([], dtype=dtype)
  1404. @pytest.mark.parametrize("unit", ["ps", "as", "fs", "Y", "M", "W", "D", "h", "m"])
  1405. @pytest.mark.parametrize("kind", ["m", "M"])
  1406. def test_constructor_generic_timestamp_bad_frequency(self, kind, unit):
  1407. # see gh-15524, gh-15987
  1408. # as of 2.0 we raise on any non-supported unit rather than silently
  1409. # cast to nanos; previously we only raised for frequencies higher
  1410. # than ns
  1411. dtype = f"{kind}8[{unit}]"
  1412. msg = "dtype=.* is not supported. Supported resolutions are"
  1413. with pytest.raises(TypeError, match=msg):
  1414. Series([], dtype=dtype)
  1415. with pytest.raises(TypeError, match=msg):
  1416. # pre-2.0 the DataFrame cast raised but the Series case did not
  1417. DataFrame([[0]], dtype=dtype)
  1418. @pytest.mark.parametrize("dtype", [None, "uint8", "category"])
  1419. def test_constructor_range_dtype(self, dtype):
  1420. # GH 16804
  1421. expected = Series([0, 1, 2, 3, 4], dtype=dtype or "int64")
  1422. result = Series(range(5), dtype=dtype)
  1423. tm.assert_series_equal(result, expected)
  1424. def test_constructor_range_overflows(self):
  1425. # GH#30173 range objects that overflow int64
  1426. rng = range(2**63, 2**63 + 4)
  1427. ser = Series(rng)
  1428. expected = Series(list(rng))
  1429. tm.assert_series_equal(ser, expected)
  1430. assert list(ser) == list(rng)
  1431. assert ser.dtype == np.uint64
  1432. rng2 = range(2**63 + 4, 2**63, -1)
  1433. ser2 = Series(rng2)
  1434. expected2 = Series(list(rng2))
  1435. tm.assert_series_equal(ser2, expected2)
  1436. assert list(ser2) == list(rng2)
  1437. assert ser2.dtype == np.uint64
  1438. rng3 = range(-(2**63), -(2**63) - 4, -1)
  1439. ser3 = Series(rng3)
  1440. expected3 = Series(list(rng3))
  1441. tm.assert_series_equal(ser3, expected3)
  1442. assert list(ser3) == list(rng3)
  1443. assert ser3.dtype == object
  1444. rng4 = range(2**73, 2**73 + 4)
  1445. ser4 = Series(rng4)
  1446. expected4 = Series(list(rng4))
  1447. tm.assert_series_equal(ser4, expected4)
  1448. assert list(ser4) == list(rng4)
  1449. assert ser4.dtype == object
  1450. def test_constructor_tz_mixed_data(self):
  1451. # GH 13051
  1452. dt_list = [
  1453. Timestamp("2016-05-01 02:03:37"),
  1454. Timestamp("2016-04-30 19:03:37-0700", tz="US/Pacific"),
  1455. ]
  1456. result = Series(dt_list)
  1457. expected = Series(dt_list, dtype=object)
  1458. tm.assert_series_equal(result, expected)
  1459. @pytest.mark.parametrize("pydt", [True, False])
  1460. def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
  1461. # GH#25843, GH#41555, GH#33401
  1462. tz = tz_aware_fixture
  1463. ts = Timestamp("2019", tz=tz)
  1464. if pydt:
  1465. ts = ts.to_pydatetime()
  1466. msg = (
  1467. "Cannot convert timezone-aware data to timezone-naive dtype. "
  1468. r"Use pd.Series\(values\).dt.tz_localize\(None\) instead."
  1469. )
  1470. with pytest.raises(ValueError, match=msg):
  1471. Series([ts], dtype="datetime64[ns]")
  1472. with pytest.raises(ValueError, match=msg):
  1473. Series(np.array([ts], dtype=object), dtype="datetime64[ns]")
  1474. with pytest.raises(ValueError, match=msg):
  1475. Series({0: ts}, dtype="datetime64[ns]")
  1476. msg = "Cannot unbox tzaware Timestamp to tznaive dtype"
  1477. with pytest.raises(TypeError, match=msg):
  1478. Series(ts, index=[0], dtype="datetime64[ns]")
  1479. def test_constructor_datetime64(self):
  1480. rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
  1481. dates = np.asarray(rng)
  1482. series = Series(dates)
  1483. assert np.issubdtype(series.dtype, np.dtype("M8[ns]"))
  1484. def test_constructor_datetimelike_scalar_to_string_dtype(
  1485. self, nullable_string_dtype
  1486. ):
  1487. # https://github.com/pandas-dev/pandas/pull/33846
  1488. result = Series("M", index=[1, 2, 3], dtype=nullable_string_dtype)
  1489. expected = Series(["M", "M", "M"], index=[1, 2, 3], dtype=nullable_string_dtype)
  1490. tm.assert_series_equal(result, expected)
  1491. @pytest.mark.parametrize(
  1492. "values",
  1493. [
  1494. [np.datetime64("2012-01-01"), np.datetime64("2013-01-01")],
  1495. ["2012-01-01", "2013-01-01"],
  1496. ],
  1497. )
  1498. def test_constructor_sparse_datetime64(self, values):
  1499. # https://github.com/pandas-dev/pandas/issues/35762
  1500. dtype = pd.SparseDtype("datetime64[ns]")
  1501. result = Series(values, dtype=dtype)
  1502. arr = pd.arrays.SparseArray(values, dtype=dtype)
  1503. expected = Series(arr)
  1504. tm.assert_series_equal(result, expected)
  1505. def test_construction_from_ordered_collection(self):
  1506. # https://github.com/pandas-dev/pandas/issues/36044
  1507. result = Series({"a": 1, "b": 2}.keys())
  1508. expected = Series(["a", "b"])
  1509. tm.assert_series_equal(result, expected)
  1510. result = Series({"a": 1, "b": 2}.values())
  1511. expected = Series([1, 2])
  1512. tm.assert_series_equal(result, expected)
  1513. def test_construction_from_large_int_scalar_no_overflow(self):
  1514. # https://github.com/pandas-dev/pandas/issues/36291
  1515. n = 1_000_000_000_000_000_000_000
  1516. result = Series(n, index=[0])
  1517. expected = Series(n)
  1518. tm.assert_series_equal(result, expected)
  1519. def test_constructor_list_of_periods_infers_period_dtype(self):
  1520. series = Series(list(period_range("2000-01-01", periods=10, freq="D")))
  1521. assert series.dtype == "Period[D]"
  1522. series = Series(
  1523. [Period("2011-01-01", freq="D"), Period("2011-02-01", freq="D")]
  1524. )
  1525. assert series.dtype == "Period[D]"
  1526. def test_constructor_subclass_dict(self, dict_subclass):
  1527. data = dict_subclass((x, 10.0 * x) for x in range(10))
  1528. series = Series(data)
  1529. expected = Series(dict(data.items()))
  1530. tm.assert_series_equal(series, expected)
  1531. def test_constructor_ordereddict(self):
  1532. # GH3283
  1533. data = OrderedDict(
  1534. (f"col{i}", np.random.default_rng(2).random()) for i in range(12)
  1535. )
  1536. series = Series(data)
  1537. expected = Series(list(data.values()), list(data.keys()))
  1538. tm.assert_series_equal(series, expected)
  1539. # Test with subclass
  1540. class A(OrderedDict):
  1541. pass
  1542. series = Series(A(data))
  1543. tm.assert_series_equal(series, expected)
  1544. def test_constructor_dict_multiindex(self):
  1545. d = {("a", "a"): 0.0, ("b", "a"): 1.0, ("b", "c"): 2.0}
  1546. _d = sorted(d.items())
  1547. result = Series(d)
  1548. expected = Series(
  1549. [x[1] for x in _d], index=MultiIndex.from_tuples([x[0] for x in _d])
  1550. )
  1551. tm.assert_series_equal(result, expected)
  1552. d["z"] = 111.0
  1553. _d.insert(0, ("z", d["z"]))
  1554. result = Series(d)
  1555. expected = Series(
  1556. [x[1] for x in _d], index=Index([x[0] for x in _d], tupleize_cols=False)
  1557. )
  1558. result = result.reindex(index=expected.index)
  1559. tm.assert_series_equal(result, expected)
  1560. def test_constructor_dict_multiindex_reindex_flat(self):
  1561. # construction involves reindexing with a MultiIndex corner case
  1562. data = {("i", "i"): 0, ("i", "j"): 1, ("j", "i"): 2, "j": np.nan}
  1563. expected = Series(data)
  1564. result = Series(expected[:-1].to_dict(), index=expected.index)
  1565. tm.assert_series_equal(result, expected)
  1566. def test_constructor_dict_timedelta_index(self):
  1567. # GH #12169 : Resample category data with timedelta index
  1568. # construct Series from dict as data and TimedeltaIndex as index
  1569. # will result NaN in result Series data
  1570. expected = Series(
  1571. data=["A", "B", "C"], index=pd.to_timedelta([0, 10, 20], unit="s")
  1572. )
  1573. result = Series(
  1574. data={
  1575. pd.to_timedelta(0, unit="s"): "A",
  1576. pd.to_timedelta(10, unit="s"): "B",
  1577. pd.to_timedelta(20, unit="s"): "C",
  1578. },
  1579. index=pd.to_timedelta([0, 10, 20], unit="s"),
  1580. )
  1581. tm.assert_series_equal(result, expected)
  1582. def test_constructor_infer_index_tz(self):
  1583. values = [188.5, 328.25]
  1584. tzinfo = tzoffset(None, 7200)
  1585. index = [
  1586. datetime(2012, 5, 11, 11, tzinfo=tzinfo),
  1587. datetime(2012, 5, 11, 12, tzinfo=tzinfo),
  1588. ]
  1589. series = Series(data=values, index=index)
  1590. assert series.index.tz == tzinfo
  1591. # it works! GH#2443
  1592. repr(series.index[0])
  1593. def test_constructor_with_pandas_dtype(self):
  1594. # going through 2D->1D path
  1595. vals = [(1,), (2,), (3,)]
  1596. ser = Series(vals)
  1597. dtype = ser.array.dtype # NumpyEADtype
  1598. ser2 = Series(vals, dtype=dtype)
  1599. tm.assert_series_equal(ser, ser2)
  1600. def test_constructor_int_dtype_missing_values(self):
  1601. # GH#43017
  1602. result = Series(index=[0], dtype="int64")
  1603. expected = Series(np.nan, index=[0], dtype="float64")
  1604. tm.assert_series_equal(result, expected)
  1605. def test_constructor_bool_dtype_missing_values(self):
  1606. # GH#43018
  1607. result = Series(index=[0], dtype="bool")
  1608. expected = Series(True, index=[0], dtype="bool")
  1609. tm.assert_series_equal(result, expected)
  1610. def test_constructor_int64_dtype(self, any_int_dtype):
  1611. # GH#44923
  1612. result = Series(["0", "1", "2"], dtype=any_int_dtype)
  1613. expected = Series([0, 1, 2], dtype=any_int_dtype)
  1614. tm.assert_series_equal(result, expected)
  1615. def test_constructor_raise_on_lossy_conversion_of_strings(self):
  1616. # GH#44923
  1617. if not np_version_gt2:
  1618. raises = pytest.raises(
  1619. ValueError, match="string values cannot be losslessly cast to int8"
  1620. )
  1621. else:
  1622. raises = pytest.raises(
  1623. OverflowError, match="The elements provided in the data"
  1624. )
  1625. with raises:
  1626. Series(["128"], dtype="int8")
  1627. def test_constructor_dtype_timedelta_alternative_construct(self):
  1628. # GH#35465
  1629. result = Series([1000000, 200000, 3000000], dtype="timedelta64[ns]")
  1630. expected = Series(pd.to_timedelta([1000000, 200000, 3000000], unit="ns"))
  1631. tm.assert_series_equal(result, expected)
  1632. @pytest.mark.xfail(
  1633. reason="Not clear what the correct expected behavior should be with "
  1634. "integers now that we support non-nano. ATM (2022-10-08) we treat ints "
  1635. "as nanoseconds, then cast to the requested dtype. xref #48312"
  1636. )
  1637. def test_constructor_dtype_timedelta_ns_s(self):
  1638. # GH#35465
  1639. result = Series([1000000, 200000, 3000000], dtype="timedelta64[ns]")
  1640. expected = Series([1000000, 200000, 3000000], dtype="timedelta64[s]")
  1641. tm.assert_series_equal(result, expected)
  1642. @pytest.mark.xfail(
  1643. reason="Not clear what the correct expected behavior should be with "
  1644. "integers now that we support non-nano. ATM (2022-10-08) we treat ints "
  1645. "as nanoseconds, then cast to the requested dtype. xref #48312"
  1646. )
  1647. def test_constructor_dtype_timedelta_ns_s_astype_int64(self):
  1648. # GH#35465
  1649. result = Series([1000000, 200000, 3000000], dtype="timedelta64[ns]").astype(
  1650. "int64"
  1651. )
  1652. expected = Series([1000000, 200000, 3000000], dtype="timedelta64[s]").astype(
  1653. "int64"
  1654. )
  1655. tm.assert_series_equal(result, expected)
  1656. @pytest.mark.filterwarnings(
  1657. "ignore:elementwise comparison failed:DeprecationWarning"
  1658. )
  1659. @pytest.mark.parametrize("func", [Series, DataFrame, Index, pd.array])
  1660. def test_constructor_mismatched_null_nullable_dtype(
  1661. self, func, any_numeric_ea_dtype
  1662. ):
  1663. # GH#44514
  1664. msg = "|".join(
  1665. [
  1666. "cannot safely cast non-equivalent object",
  1667. r"int\(\) argument must be a string, a bytes-like object "
  1668. "or a (real )?number",
  1669. r"Cannot cast array data from dtype\('O'\) to dtype\('float64'\) "
  1670. "according to the rule 'safe'",
  1671. "object cannot be converted to a FloatingDtype",
  1672. "'values' contains non-numeric NA",
  1673. ]
  1674. )
  1675. for null in tm.NP_NAT_OBJECTS + [NaT]:
  1676. with pytest.raises(TypeError, match=msg):
  1677. func([null, 1.0, 3.0], dtype=any_numeric_ea_dtype)
  1678. def test_series_constructor_ea_int_from_bool(self):
  1679. # GH#42137
  1680. result = Series([True, False, True, pd.NA], dtype="Int64")
  1681. expected = Series([1, 0, 1, pd.NA], dtype="Int64")
  1682. tm.assert_series_equal(result, expected)
  1683. result = Series([True, False, True], dtype="Int64")
  1684. expected = Series([1, 0, 1], dtype="Int64")
  1685. tm.assert_series_equal(result, expected)
  1686. def test_series_constructor_ea_int_from_string_bool(self):
  1687. # GH#42137
  1688. with pytest.raises(ValueError, match="invalid literal"):
  1689. Series(["True", "False", "True", pd.NA], dtype="Int64")
  1690. @pytest.mark.parametrize("val", [1, 1.0])
  1691. def test_series_constructor_overflow_uint_ea(self, val):
  1692. # GH#38798
  1693. max_val = np.iinfo(np.uint64).max - 1
  1694. result = Series([max_val, val], dtype="UInt64")
  1695. expected = Series(np.array([max_val, 1], dtype="uint64"), dtype="UInt64")
  1696. tm.assert_series_equal(result, expected)
  1697. @pytest.mark.parametrize("val", [1, 1.0])
  1698. def test_series_constructor_overflow_uint_ea_with_na(self, val):
  1699. # GH#38798
  1700. max_val = np.iinfo(np.uint64).max - 1
  1701. result = Series([max_val, val, pd.NA], dtype="UInt64")
  1702. expected = Series(
  1703. IntegerArray(
  1704. np.array([max_val, 1, 0], dtype="uint64"),
  1705. np.array([0, 0, 1], dtype=np.bool_),
  1706. )
  1707. )
  1708. tm.assert_series_equal(result, expected)
  1709. def test_series_constructor_overflow_uint_with_nan(self):
  1710. # GH#38798
  1711. max_val = np.iinfo(np.uint64).max - 1
  1712. result = Series([max_val, np.nan], dtype="UInt64")
  1713. expected = Series(
  1714. IntegerArray(
  1715. np.array([max_val, 1], dtype="uint64"),
  1716. np.array([0, 1], dtype=np.bool_),
  1717. )
  1718. )
  1719. tm.assert_series_equal(result, expected)
  1720. def test_series_constructor_ea_all_na(self):
  1721. # GH#38798
  1722. result = Series([np.nan, np.nan], dtype="UInt64")
  1723. expected = Series(
  1724. IntegerArray(
  1725. np.array([1, 1], dtype="uint64"),
  1726. np.array([1, 1], dtype=np.bool_),
  1727. )
  1728. )
  1729. tm.assert_series_equal(result, expected)
  1730. def test_series_from_index_dtype_equal_does_not_copy(self):
  1731. # GH#52008
  1732. idx = Index([1, 2, 3])
  1733. expected = idx.copy(deep=True)
  1734. ser = Series(idx, dtype="int64")
  1735. ser.iloc[0] = 100
  1736. tm.assert_index_equal(idx, expected)
  1737. def test_series_string_inference(self):
  1738. # GH#54430
  1739. with pd.option_context("future.infer_string", True):
  1740. ser = Series(["a", "b"])
  1741. dtype = pd.StringDtype("pyarrow" if HAS_PYARROW else "python", na_value=np.nan)
  1742. expected = Series(["a", "b"], dtype=dtype)
  1743. tm.assert_series_equal(ser, expected)
  1744. expected = Series(["a", 1], dtype="object")
  1745. with pd.option_context("future.infer_string", True):
  1746. ser = Series(["a", 1])
  1747. tm.assert_series_equal(ser, expected)
  1748. @pytest.mark.parametrize("na_value", [None, np.nan, pd.NA])
  1749. def test_series_string_with_na_inference(self, na_value):
  1750. # GH#54430
  1751. with pd.option_context("future.infer_string", True):
  1752. ser = Series(["a", na_value])
  1753. dtype = pd.StringDtype("pyarrow" if HAS_PYARROW else "python", na_value=np.nan)
  1754. expected = Series(["a", None], dtype=dtype)
  1755. tm.assert_series_equal(ser, expected)
  1756. def test_series_string_inference_scalar(self):
  1757. # GH#54430
  1758. with pd.option_context("future.infer_string", True):
  1759. ser = Series("a", index=[1])
  1760. dtype = pd.StringDtype("pyarrow" if HAS_PYARROW else "python", na_value=np.nan)
  1761. expected = Series("a", index=[1], dtype=dtype)
  1762. tm.assert_series_equal(ser, expected)
  1763. def test_series_string_inference_array_string_dtype(self):
  1764. # GH#54496
  1765. with pd.option_context("future.infer_string", True):
  1766. ser = Series(np.array(["a", "b"]))
  1767. dtype = pd.StringDtype("pyarrow" if HAS_PYARROW else "python", na_value=np.nan)
  1768. expected = Series(["a", "b"], dtype=dtype)
  1769. tm.assert_series_equal(ser, expected)
  1770. def test_series_string_inference_storage_definition(self):
  1771. # https://github.com/pandas-dev/pandas/issues/54793
  1772. # but after PDEP-14 (string dtype), it was decided to keep dtype="string"
  1773. # returning the NA string dtype, so expected is changed from
  1774. # "string[pyarrow_numpy]" to "string[python]"
  1775. expected = Series(["a", "b"], dtype="string[python]")
  1776. with pd.option_context("future.infer_string", True):
  1777. result = Series(["a", "b"], dtype="string")
  1778. tm.assert_series_equal(result, expected)
  1779. expected = Series(["a", "b"], dtype=pd.StringDtype(na_value=np.nan))
  1780. with pd.option_context("future.infer_string", True):
  1781. result = Series(["a", "b"], dtype="str")
  1782. tm.assert_series_equal(result, expected)
  1783. def test_series_constructor_infer_string_scalar(self):
  1784. # GH#55537
  1785. with pd.option_context("future.infer_string", True):
  1786. ser = Series("a", index=[1, 2], dtype="string[python]")
  1787. expected = Series(["a", "a"], index=[1, 2], dtype="string[python]")
  1788. tm.assert_series_equal(ser, expected)
  1789. assert ser.dtype.storage == "python"
  1790. def test_series_string_inference_na_first(self):
  1791. # GH#55655
  1792. with pd.option_context("future.infer_string", True):
  1793. result = Series([pd.NA, "b"])
  1794. dtype = pd.StringDtype("pyarrow" if HAS_PYARROW else "python", na_value=np.nan)
  1795. expected = Series([None, "b"], dtype=dtype)
  1796. tm.assert_series_equal(result, expected)
  1797. def test_inference_on_pandas_objects(self):
  1798. # GH#56012
  1799. ser = Series([Timestamp("2019-12-31")], dtype=object)
  1800. with tm.assert_produces_warning(None):
  1801. # This doesn't do inference
  1802. result = Series(ser)
  1803. assert result.dtype == np.object_
  1804. idx = Index([Timestamp("2019-12-31")], dtype=object)
  1805. with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
  1806. result = Series(idx)
  1807. assert result.dtype != np.object_
  1808. class TestSeriesConstructorIndexCoercion:
  1809. def test_series_constructor_datetimelike_index_coercion(self):
  1810. idx = date_range("2020-01-01", periods=5)
  1811. ser = Series(
  1812. np.random.default_rng(2).standard_normal(len(idx)), idx.astype(object)
  1813. )
  1814. # as of 2.0, we no longer silently cast the object-dtype index
  1815. # to DatetimeIndex GH#39307, GH#23598
  1816. assert not isinstance(ser.index, DatetimeIndex)
  1817. @pytest.mark.parametrize("container", [None, np.array, Series, Index])
  1818. @pytest.mark.parametrize("data", [1.0, range(4)])
  1819. def test_series_constructor_infer_multiindex(self, container, data):
  1820. indexes = [["a", "a", "b", "b"], ["x", "y", "x", "y"]]
  1821. if container is not None:
  1822. indexes = [container(ind) for ind in indexes]
  1823. multi = Series(data, index=indexes)
  1824. assert isinstance(multi.index, MultiIndex)
  1825. # TODO: make this not cast to object in pandas 3.0
  1826. @pytest.mark.skipif(
  1827. not np_version_gt2, reason="StringDType only available in numpy 2 and above"
  1828. )
  1829. @pytest.mark.parametrize(
  1830. "data",
  1831. [
  1832. ["a", "b", "c"],
  1833. ["a", "b", np.nan],
  1834. ],
  1835. )
  1836. def test_np_string_array_object_cast(self, data):
  1837. from numpy.dtypes import StringDType
  1838. arr = np.array(data, dtype=StringDType())
  1839. res = Series(arr)
  1840. assert res.dtype == np.object_
  1841. assert (res == data).all()
  1842. class TestSeriesConstructorInternals:
  1843. def test_constructor_no_pandas_array(self, using_array_manager):
  1844. ser = Series([1, 2, 3])
  1845. result = Series(ser.array)
  1846. tm.assert_series_equal(ser, result)
  1847. if not using_array_manager:
  1848. assert isinstance(result._mgr.blocks[0], NumpyBlock)
  1849. assert result._mgr.blocks[0].is_numeric
  1850. @td.skip_array_manager_invalid_test
  1851. def test_from_array(self):
  1852. result = Series(pd.array(["1h", "2h"], dtype="timedelta64[ns]"))
  1853. assert result._mgr.blocks[0].is_extension is False
  1854. result = Series(pd.array(["2015"], dtype="datetime64[ns]"))
  1855. assert result._mgr.blocks[0].is_extension is False
  1856. @td.skip_array_manager_invalid_test
  1857. def test_from_list_dtype(self):
  1858. result = Series(["1h", "2h"], dtype="timedelta64[ns]")
  1859. assert result._mgr.blocks[0].is_extension is False
  1860. result = Series(["2015"], dtype="datetime64[ns]")
  1861. assert result._mgr.blocks[0].is_extension is False
  1862. def test_constructor(rand_series_with_duplicate_datetimeindex):
  1863. dups = rand_series_with_duplicate_datetimeindex
  1864. assert isinstance(dups, Series)
  1865. assert isinstance(dups.index, DatetimeIndex)
  1866. @pytest.mark.parametrize(
  1867. "input_dict,expected",
  1868. [
  1869. ({0: 0}, np.array([[0]], dtype=np.int64)),
  1870. ({"a": "a"}, np.array([["a"]], dtype=object)),
  1871. ({1: 1}, np.array([[1]], dtype=np.int64)),
  1872. ],
  1873. )
  1874. def test_numpy_array(input_dict, expected):
  1875. result = np.array([Series(input_dict)])
  1876. tm.assert_numpy_array_equal(result, expected)
  1877. def test_index_ordered_dict_keys():
  1878. # GH 22077
  1879. param_index = OrderedDict(
  1880. [
  1881. ((("a", "b"), ("c", "d")), 1),
  1882. ((("a", None), ("c", "d")), 2),
  1883. ]
  1884. )
  1885. series = Series([1, 2], index=param_index.keys())
  1886. expected = Series(
  1887. [1, 2],
  1888. index=MultiIndex.from_tuples(
  1889. [(("a", "b"), ("c", "d")), (("a", None), ("c", "d"))]
  1890. ),
  1891. )
  1892. tm.assert_series_equal(series, expected)
  1893. @pytest.mark.parametrize(
  1894. "input_list",
  1895. [
  1896. [1, complex("nan"), 2],
  1897. [1 + 1j, complex("nan"), 2 + 2j],
  1898. ],
  1899. )
  1900. def test_series_with_complex_nan(input_list):
  1901. # GH#53627
  1902. ser = Series(input_list)
  1903. result = Series(ser.array)
  1904. assert ser.dtype == "complex128"
  1905. tm.assert_series_equal(ser, result)