Best Python code snippet using localstack_python
test_internals.py
Source:test_internals.py
...6import pandas._testing as tm7from pandas.core.internals.blocks import IntBlock8class TestSeriesInternals:9 # GH 1026510 def test_convert(self):11 # Tests: All to nans, coerce, true12 # Test coercion returns correct type13 s = Series(["a", "b", "c"])14 results = s._convert(datetime=True, coerce=True)15 expected = Series([NaT] * 3)16 tm.assert_series_equal(results, expected)17 results = s._convert(numeric=True, coerce=True)18 expected = Series([np.nan] * 3)19 tm.assert_series_equal(results, expected)20 expected = Series([NaT] * 3, dtype=np.dtype("m8[ns]"))21 results = s._convert(timedelta=True, coerce=True)22 tm.assert_series_equal(results, expected)23 dt = datetime(2001, 1, 1, 0, 0)24 td = dt - datetime(2000, 1, 1, 0, 0)25 # Test coercion with mixed types26 s = Series(["a", "3.1415", dt, td])27 results = s._convert(datetime=True, coerce=True)28 expected = Series([NaT, NaT, dt, NaT])29 tm.assert_series_equal(results, expected)30 results = s._convert(numeric=True, coerce=True)31 expected = Series([np.nan, 3.1415, np.nan, np.nan])32 tm.assert_series_equal(results, expected)33 results = s._convert(timedelta=True, coerce=True)34 expected = Series([NaT, NaT, NaT, td], dtype=np.dtype("m8[ns]"))35 tm.assert_series_equal(results, expected)36 # Test standard conversion returns original37 results = s._convert(datetime=True)38 tm.assert_series_equal(results, s)39 results = s._convert(numeric=True)40 expected = Series([np.nan, 3.1415, np.nan, np.nan])41 tm.assert_series_equal(results, expected)42 results = s._convert(timedelta=True)43 tm.assert_series_equal(results, s)44 # test pass-through and non-conversion when other types selected45 s = Series(["1.0", "2.0", "3.0"])46 results = s._convert(datetime=True, numeric=True, timedelta=True)47 expected = Series([1.0, 2.0, 3.0])48 tm.assert_series_equal(results, expected)49 results = s._convert(True, False, True)50 tm.assert_series_equal(results, s)51 s = Series([datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 1, 0, 0)], dtype="O")52 results = s._convert(datetime=True, numeric=True, timedelta=True)53 expected = Series([datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 1, 0, 0)])54 tm.assert_series_equal(results, expected)55 results = s._convert(datetime=False, numeric=True, timedelta=True)56 tm.assert_series_equal(results, s)57 td = datetime(2001, 1, 1, 0, 0) - datetime(2000, 1, 1, 0, 0)58 s = Series([td, td], dtype="O")59 results = s._convert(datetime=True, numeric=True, timedelta=True)60 expected = Series([td, td])61 tm.assert_series_equal(results, expected)62 results = s._convert(True, True, False)63 tm.assert_series_equal(results, s)64 s = Series([1.0, 2, 3], index=["a", "b", "c"])65 result = s._convert(numeric=True)66 tm.assert_series_equal(result, s)67 # force numeric conversion68 r = s.copy().astype("O")69 r["a"] = "1"70 result = r._convert(numeric=True)71 tm.assert_series_equal(result, s)72 r = s.copy().astype("O")73 r["a"] = "1."74 result = r._convert(numeric=True)75 tm.assert_series_equal(result, s)76 r = s.copy().astype("O")77 r["a"] = "garbled"78 result = r._convert(numeric=True)79 expected = s.copy()80 expected["a"] = np.nan81 tm.assert_series_equal(result, expected)82 # GH 4119, not converting a mixed type (e.g.floats and object)83 s = Series([1, "na", 3, 4])84 result = s._convert(datetime=True, numeric=True)85 expected = Series([1, np.nan, 3, 4])86 tm.assert_series_equal(result, expected)87 s = Series([1, "", 3, 4])88 result = s._convert(datetime=True, numeric=True)89 tm.assert_series_equal(result, expected)90 # dates91 s = Series(92 [93 datetime(2001, 1, 1, 0, 0),94 datetime(2001, 1, 2, 0, 0),95 datetime(2001, 1, 3, 0, 0),96 ]97 )98 s2 = Series(99 [100 datetime(2001, 1, 1, 0, 0),101 datetime(2001, 1, 2, 0, 0),102 datetime(2001, 1, 3, 0, 0),103 "foo",104 1.0,105 1,106 Timestamp("20010104"),107 "20010105",108 ],109 dtype="O",110 )111 result = s._convert(datetime=True)112 expected = Series(113 [Timestamp("20010101"), Timestamp("20010102"), Timestamp("20010103")],114 dtype="M8[ns]",115 )116 tm.assert_series_equal(result, expected)117 result = s._convert(datetime=True, coerce=True)118 tm.assert_series_equal(result, expected)119 expected = Series(120 [121 Timestamp("20010101"),122 Timestamp("20010102"),123 Timestamp("20010103"),124 NaT,125 NaT,126 NaT,127 Timestamp("20010104"),128 Timestamp("20010105"),129 ],130 dtype="M8[ns]",131 )132 result = s2._convert(datetime=True, numeric=False, timedelta=False, coerce=True)133 tm.assert_series_equal(result, expected)134 result = s2._convert(datetime=True, coerce=True)135 tm.assert_series_equal(result, expected)136 s = Series(["foo", "bar", 1, 1.0], dtype="O")137 result = s._convert(datetime=True, coerce=True)138 expected = Series([NaT] * 2 + [Timestamp(1)] * 2)139 tm.assert_series_equal(result, expected)140 # preserver if non-object141 s = Series([1], dtype="float32")142 result = s._convert(datetime=True, coerce=True)143 tm.assert_series_equal(result, s)144 # FIXME: dont leave commented-out145 # r = s.copy()146 # r[0] = np.nan147 # result = r._convert(convert_dates=True,convert_numeric=False)148 # assert result.dtype == 'M8[ns]'149 # dateutil parses some single letters into today's value as a date150 expected = Series([NaT])151 for x in "abcdefghijklmnopqrstuvwxyz":152 s = Series([x])153 result = s._convert(datetime=True, coerce=True)154 tm.assert_series_equal(result, expected)155 s = Series([x.upper()])156 result = s._convert(datetime=True, coerce=True)157 tm.assert_series_equal(result, expected)158 def test_convert_no_arg_error(self):159 s = Series(["1.0", "2"])160 msg = r"At least one of datetime, numeric or timedelta must be True\."161 with pytest.raises(ValueError, match=msg):162 s._convert()163 def test_convert_preserve_bool(self):164 s = Series([1, True, 3, 5], dtype=object)165 r = s._convert(datetime=True, numeric=True)166 e = Series([1, 1, 3, 5], dtype="i8")167 tm.assert_series_equal(r, e)168 def test_convert_preserve_all_bool(self):169 s = Series([False, True, False, False], dtype=object)170 r = s._convert(datetime=True, numeric=True)171 e = Series([False, True, False, False], dtype=bool)172 tm.assert_series_equal(r, e)173 def test_constructor_no_pandas_array(self):174 ser = pd.Series([1, 2, 3])175 result = pd.Series(ser.array)176 tm.assert_series_equal(ser, result)177 assert isinstance(result._mgr.blocks[0], IntBlock)178 def test_astype_no_pandas_dtype(self):179 # https://github.com/pandas-dev/pandas/pull/24866180 ser = pd.Series([1, 2], dtype="int64")181 # Don't have PandasDtype in the public API, so we use `.array.dtype`,182 # which is a PandasDtype.183 result = ser.astype(ser.array.dtype)184 tm.assert_series_equal(result, ser)...
test_convert.py
Source:test_convert.py
...6 Timestamp,7)8import pandas._testing as tm9class TestConvert:10 def test_convert(self):11 # GH#1026512 dt = datetime(2001, 1, 1, 0, 0)13 td = dt - datetime(2000, 1, 1, 0, 0)14 # Test coercion with mixed types15 ser = Series(["a", "3.1415", dt, td])16 results = ser._convert(numeric=True)17 expected = Series([np.nan, 3.1415, np.nan, np.nan])18 tm.assert_series_equal(results, expected)19 # Test standard conversion returns original20 results = ser._convert(datetime=True)21 tm.assert_series_equal(results, ser)22 results = ser._convert(numeric=True)23 expected = Series([np.nan, 3.1415, np.nan, np.nan])24 tm.assert_series_equal(results, expected)25 results = ser._convert(timedelta=True)26 tm.assert_series_equal(results, ser)27 # test pass-through and non-conversion when other types selected28 ser = Series(["1.0", "2.0", "3.0"])29 results = ser._convert(datetime=True, numeric=True, timedelta=True)30 expected = Series([1.0, 2.0, 3.0])31 tm.assert_series_equal(results, expected)32 results = ser._convert(True, False, True)33 tm.assert_series_equal(results, ser)34 ser = Series(35 [datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 1, 0, 0)], dtype="O"36 )37 results = ser._convert(datetime=True, numeric=True, timedelta=True)38 expected = Series([datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 1, 0, 0)])39 tm.assert_series_equal(results, expected)40 results = ser._convert(datetime=False, numeric=True, timedelta=True)41 tm.assert_series_equal(results, ser)42 td = datetime(2001, 1, 1, 0, 0) - datetime(2000, 1, 1, 0, 0)43 ser = Series([td, td], dtype="O")44 results = ser._convert(datetime=True, numeric=True, timedelta=True)45 expected = Series([td, td])46 tm.assert_series_equal(results, expected)47 results = ser._convert(True, True, False)48 tm.assert_series_equal(results, ser)49 ser = Series([1.0, 2, 3], index=["a", "b", "c"])50 result = ser._convert(numeric=True)51 tm.assert_series_equal(result, ser)52 # force numeric conversion53 res = ser.copy().astype("O")54 res["a"] = "1"55 result = res._convert(numeric=True)56 tm.assert_series_equal(result, ser)57 res = ser.copy().astype("O")58 res["a"] = "1."59 result = res._convert(numeric=True)60 tm.assert_series_equal(result, ser)61 res = ser.copy().astype("O")62 res["a"] = "garbled"63 result = res._convert(numeric=True)64 expected = ser.copy()65 expected["a"] = np.nan66 tm.assert_series_equal(result, expected)67 # GH 4119, not converting a mixed type (e.g.floats and object)68 ser = Series([1, "na", 3, 4])69 result = ser._convert(datetime=True, numeric=True)70 expected = Series([1, np.nan, 3, 4])71 tm.assert_series_equal(result, expected)72 ser = Series([1, "", 3, 4])73 result = ser._convert(datetime=True, numeric=True)74 tm.assert_series_equal(result, expected)75 # dates76 ser = Series(77 [78 datetime(2001, 1, 1, 0, 0),79 datetime(2001, 1, 2, 0, 0),80 datetime(2001, 1, 3, 0, 0),81 ]82 )83 result = ser._convert(datetime=True)84 expected = Series(85 [Timestamp("20010101"), Timestamp("20010102"), Timestamp("20010103")],86 dtype="M8[ns]",87 )88 tm.assert_series_equal(result, expected)89 result = ser._convert(datetime=True)90 tm.assert_series_equal(result, expected)91 # preserver if non-object92 ser = Series([1], dtype="float32")93 result = ser._convert(datetime=True)94 tm.assert_series_equal(result, ser)95 # FIXME: dont leave commented-out96 # res = ser.copy()97 # r[0] = np.nan98 # result = res._convert(convert_dates=True,convert_numeric=False)99 # assert result.dtype == 'M8[ns]'100 def test_convert_no_arg_error(self):101 ser = Series(["1.0", "2"])102 msg = r"At least one of datetime, numeric or timedelta must be True\."103 with pytest.raises(ValueError, match=msg):104 ser._convert()105 def test_convert_preserve_bool(self):106 ser = Series([1, True, 3, 5], dtype=object)107 res = ser._convert(datetime=True, numeric=True)108 expected = Series([1, 1, 3, 5], dtype="i8")109 tm.assert_series_equal(res, expected)110 def test_convert_preserve_all_bool(self):111 ser = Series([False, True, False, False], dtype=object)112 res = ser._convert(datetime=True, numeric=True)113 expected = Series([False, True, False, False], dtype=bool)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!