1""" 2test setting *parts* of objects both positionally and label based 3 4TODO: these should be split among the indexer tests 5""" 6 7import numpy as np 8import pytest 9 10import pandas as pd 11from pandas import DataFrame, Index, Period, Series, Timestamp, date_range, period_range 12import pandas._testing as tm 13 14 15class TestPartialSetting: 16 def test_partial_setting(self): 17 18 # GH2578, allow ix and friends to partially set 19 20 # series 21 s_orig = Series([1, 2, 3]) 22 23 s = s_orig.copy() 24 s[5] = 5 25 expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5]) 26 tm.assert_series_equal(s, expected) 27 28 s = s_orig.copy() 29 s.loc[5] = 5 30 expected = Series([1, 2, 3, 5], index=[0, 1, 2, 5]) 31 tm.assert_series_equal(s, expected) 32 33 s = s_orig.copy() 34 s[5] = 5.0 35 expected = Series([1, 2, 3, 5.0], index=[0, 1, 2, 5]) 36 tm.assert_series_equal(s, expected) 37 38 s = s_orig.copy() 39 s.loc[5] = 5.0 40 expected = Series([1, 2, 3, 5.0], index=[0, 1, 2, 5]) 41 tm.assert_series_equal(s, expected) 42 43 # iloc/iat raise 44 s = s_orig.copy() 45 46 msg = "iloc cannot enlarge its target object" 47 with pytest.raises(IndexError, match=msg): 48 s.iloc[3] = 5.0 49 50 msg = "index 3 is out of bounds for axis 0 with size 3" 51 with pytest.raises(IndexError, match=msg): 52 s.iat[3] = 5.0 53 54 # ## frame ## 55 56 df_orig = DataFrame( 57 np.arange(6).reshape(3, 2), columns=["A", "B"], dtype="int64" 58 ) 59 60 # iloc/iat raise 61 df = df_orig.copy() 62 63 msg = "iloc cannot enlarge its target object" 64 with pytest.raises(IndexError, match=msg): 65 df.iloc[4, 2] = 5.0 66 67 msg = "index 2 is out of bounds for axis 0 with size 2" 68 with pytest.raises(IndexError, match=msg): 69 df.iat[4, 2] = 5.0 70 71 # row setting where it exists 72 expected = DataFrame(dict({"A": [0, 4, 4], "B": [1, 5, 5]})) 73 df = df_orig.copy() 74 df.iloc[1] = df.iloc[2] 75 tm.assert_frame_equal(df, expected) 76 77 expected = DataFrame(dict({"A": [0, 4, 4], "B": [1, 5, 5]})) 78 df = df_orig.copy() 79 df.loc[1] = df.loc[2] 80 tm.assert_frame_equal(df, expected) 81 82 # like 2578, partial setting with dtype preservation 83 expected = DataFrame(dict({"A": [0, 2, 4, 4], "B": [1, 3, 5, 5]})) 84 df = df_orig.copy() 85 df.loc[3] = df.loc[2] 86 tm.assert_frame_equal(df, expected) 87 88 # single dtype frame, overwrite 89 expected = DataFrame(dict({"A": [0, 2, 4], "B": [0, 2, 4]})) 90 df = df_orig.copy() 91 df.loc[:, "B"] = df.loc[:, "A"] 92 tm.assert_frame_equal(df, expected) 93 94 # mixed dtype frame, overwrite 95 expected = DataFrame(dict({"A": [0, 2, 4], "B": Series([0, 2, 4])})) 96 df = df_orig.copy() 97 df["B"] = df["B"].astype(np.float64) 98 df.loc[:, "B"] = df.loc[:, "A"] 99 tm.assert_frame_equal(df, expected) 100 101 # single dtype frame, partial setting 102 expected = df_orig.copy() 103 expected["C"] = df["A"] 104 df = df_orig.copy() 105 df.loc[:, "C"] = df.loc[:, "A"] 106 tm.assert_frame_equal(df, expected) 107 108 # mixed frame, partial setting 109 expected = df_orig.copy() 110 expected["C"] = df["A"] 111 df = df_orig.copy() 112 df.loc[:, "C"] = df.loc[:, "A"] 113 tm.assert_frame_equal(df, expected) 114 115 # GH 8473 116 dates = date_range("1/1/2000", periods=8) 117 df_orig = DataFrame( 118 np.random.randn(8, 4), index=dates, columns=["A", "B", "C", "D"] 119 ) 120 121 expected = pd.concat( 122 [df_orig, DataFrame({"A": 7}, index=dates[-1:] + dates.freq)], sort=True 123 ) 124 df = df_orig.copy() 125 df.loc[dates[-1] + dates.freq, "A"] = 7 126 tm.assert_frame_equal(df, expected) 127 df = df_orig.copy() 128 df.at[dates[-1] + dates.freq, "A"] = 7 129 tm.assert_frame_equal(df, expected) 130 131 exp_other = DataFrame({0: 7}, index=dates[-1:] + dates.freq) 132 expected = pd.concat([df_orig, exp_other], axis=1) 133 134 df = df_orig.copy() 135 df.loc[dates[-1] + dates.freq, 0] = 7 136 tm.assert_frame_equal(df, expected) 137 df = df_orig.copy() 138 df.at[dates[-1] + dates.freq, 0] = 7 139 tm.assert_frame_equal(df, expected) 140 141 def test_partial_setting_mixed_dtype(self): 142 143 # in a mixed dtype environment, try to preserve dtypes 144 # by appending 145 df = DataFrame([[True, 1], [False, 2]], columns=["female", "fitness"]) 146 147 s = df.loc[1].copy() 148 s.name = 2 149 expected = df.append(s) 150 151 df.loc[2] = df.loc[1] 152 tm.assert_frame_equal(df, expected) 153 154 # columns will align 155 df = DataFrame(columns=["A", "B"]) 156 df.loc[0] = Series(1, index=range(4)) 157 tm.assert_frame_equal(df, DataFrame(columns=["A", "B"], index=[0])) 158 159 # columns will align 160 df = DataFrame(columns=["A", "B"]) 161 df.loc[0] = Series(1, index=["B"]) 162 163 exp = DataFrame([[np.nan, 1]], columns=["A", "B"], index=[0], dtype="float64") 164 tm.assert_frame_equal(df, exp) 165 166 # list-like must conform 167 df = DataFrame(columns=["A", "B"]) 168 169 msg = "cannot set a row with mismatched columns" 170 with pytest.raises(ValueError, match=msg): 171 df.loc[0] = [1, 2, 3] 172 173 # TODO: #15657, these are left as object and not coerced 174 df = DataFrame(columns=["A", "B"]) 175 df.loc[3] = [6, 7] 176 177 exp = DataFrame([[6, 7]], index=[3], columns=["A", "B"], dtype="object") 178 tm.assert_frame_equal(df, exp) 179 180 def test_series_partial_set(self): 181 # partial set with new index 182 # Regression from GH4825 183 ser = Series([0.1, 0.2], index=[1, 2]) 184 185 # loc equiv to .reindex 186 expected = Series([np.nan, 0.2, np.nan], index=[3, 2, 3]) 187 with pytest.raises(KeyError, match="with any missing labels"): 188 ser.loc[[3, 2, 3]] 189 190 result = ser.reindex([3, 2, 3]) 191 tm.assert_series_equal(result, expected, check_index_type=True) 192 193 expected = Series([np.nan, 0.2, np.nan, np.nan], index=[3, 2, 3, "x"]) 194 with pytest.raises(KeyError, match="with any missing labels"): 195 ser.loc[[3, 2, 3, "x"]] 196 197 result = ser.reindex([3, 2, 3, "x"]) 198 tm.assert_series_equal(result, expected, check_index_type=True) 199 200 expected = Series([0.2, 0.2, 0.1], index=[2, 2, 1]) 201 result = ser.loc[[2, 2, 1]] 202 tm.assert_series_equal(result, expected, check_index_type=True) 203 204 expected = Series([0.2, 0.2, np.nan, 0.1], index=[2, 2, "x", 1]) 205 with pytest.raises(KeyError, match="with any missing labels"): 206 ser.loc[[2, 2, "x", 1]] 207 208 result = ser.reindex([2, 2, "x", 1]) 209 tm.assert_series_equal(result, expected, check_index_type=True) 210 211 # raises as nothing is in the index 212 msg = ( 213 r"\"None of \[Int64Index\(\[3, 3, 3\], dtype='int64'\)\] are " 214 r"in the \[index\]\"" 215 ) 216 with pytest.raises(KeyError, match=msg): 217 ser.loc[[3, 3, 3]] 218 219 expected = Series([0.2, 0.2, np.nan], index=[2, 2, 3]) 220 with pytest.raises(KeyError, match="with any missing labels"): 221 ser.loc[[2, 2, 3]] 222 223 result = ser.reindex([2, 2, 3]) 224 tm.assert_series_equal(result, expected, check_index_type=True) 225 226 s = Series([0.1, 0.2, 0.3], index=[1, 2, 3]) 227 expected = Series([0.3, np.nan, np.nan], index=[3, 4, 4]) 228 with pytest.raises(KeyError, match="with any missing labels"): 229 s.loc[[3, 4, 4]] 230 231 result = s.reindex([3, 4, 4]) 232 tm.assert_series_equal(result, expected, check_index_type=True) 233 234 s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) 235 expected = Series([np.nan, 0.3, 0.3], index=[5, 3, 3]) 236 with pytest.raises(KeyError, match="with any missing labels"): 237 s.loc[[5, 3, 3]] 238 239 result = s.reindex([5, 3, 3]) 240 tm.assert_series_equal(result, expected, check_index_type=True) 241 242 s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) 243 expected = Series([np.nan, 0.4, 0.4], index=[5, 4, 4]) 244 with pytest.raises(KeyError, match="with any missing labels"): 245 s.loc[[5, 4, 4]] 246 247 result = s.reindex([5, 4, 4]) 248 tm.assert_series_equal(result, expected, check_index_type=True) 249 250 s = Series([0.1, 0.2, 0.3, 0.4], index=[4, 5, 6, 7]) 251 expected = Series([0.4, np.nan, np.nan], index=[7, 2, 2]) 252 with pytest.raises(KeyError, match="with any missing labels"): 253 s.loc[[7, 2, 2]] 254 255 result = s.reindex([7, 2, 2]) 256 tm.assert_series_equal(result, expected, check_index_type=True) 257 258 s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) 259 expected = Series([0.4, np.nan, np.nan], index=[4, 5, 5]) 260 with pytest.raises(KeyError, match="with any missing labels"): 261 s.loc[[4, 5, 5]] 262 263 result = s.reindex([4, 5, 5]) 264 tm.assert_series_equal(result, expected, check_index_type=True) 265 266 # iloc 267 expected = Series([0.2, 0.2, 0.1, 0.1], index=[2, 2, 1, 1]) 268 result = ser.iloc[[1, 1, 0, 0]] 269 tm.assert_series_equal(result, expected, check_index_type=True) 270 271 def test_series_partial_set_with_name(self): 272 # GH 11497 273 274 idx = Index([1, 2], dtype="int64", name="idx") 275 ser = Series([0.1, 0.2], index=idx, name="s") 276 277 # loc 278 with pytest.raises(KeyError, match="with any missing labels"): 279 ser.loc[[3, 2, 3]] 280 281 with pytest.raises(KeyError, match="with any missing labels"): 282 ser.loc[[3, 2, 3, "x"]] 283 284 exp_idx = Index([2, 2, 1], dtype="int64", name="idx") 285 expected = Series([0.2, 0.2, 0.1], index=exp_idx, name="s") 286 result = ser.loc[[2, 2, 1]] 287 tm.assert_series_equal(result, expected, check_index_type=True) 288 289 with pytest.raises(KeyError, match="with any missing labels"): 290 ser.loc[[2, 2, "x", 1]] 291 292 # raises as nothing is in the index 293 msg = ( 294 r"\"None of \[Int64Index\(\[3, 3, 3\], dtype='int64', " 295 r"name='idx'\)\] are in the \[index\]\"" 296 ) 297 with pytest.raises(KeyError, match=msg): 298 ser.loc[[3, 3, 3]] 299 300 with pytest.raises(KeyError, match="with any missing labels"): 301 ser.loc[[2, 2, 3]] 302 303 idx = Index([1, 2, 3], dtype="int64", name="idx") 304 with pytest.raises(KeyError, match="with any missing labels"): 305 Series([0.1, 0.2, 0.3], index=idx, name="s").loc[[3, 4, 4]] 306 307 idx = Index([1, 2, 3, 4], dtype="int64", name="idx") 308 with pytest.raises(KeyError, match="with any missing labels"): 309 Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 3, 3]] 310 311 idx = Index([1, 2, 3, 4], dtype="int64", name="idx") 312 with pytest.raises(KeyError, match="with any missing labels"): 313 Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 4, 4]] 314 315 idx = Index([4, 5, 6, 7], dtype="int64", name="idx") 316 with pytest.raises(KeyError, match="with any missing labels"): 317 Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[7, 2, 2]] 318 319 idx = Index([1, 2, 3, 4], dtype="int64", name="idx") 320 with pytest.raises(KeyError, match="with any missing labels"): 321 Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[4, 5, 5]] 322 323 # iloc 324 exp_idx = Index([2, 2, 1, 1], dtype="int64", name="idx") 325 expected = Series([0.2, 0.2, 0.1, 0.1], index=exp_idx, name="s") 326 result = ser.iloc[[1, 1, 0, 0]] 327 tm.assert_series_equal(result, expected, check_index_type=True) 328 329 def test_partial_set_invalid(self): 330 331 # GH 4940 332 # allow only setting of 'valid' values 333 334 orig = tm.makeTimeDataFrame() 335 df = orig.copy() 336 337 # don't allow not string inserts 338 msg = r"value should be a 'Timestamp' or 'NaT'\. Got '.*' instead\." 339 340 with pytest.raises(TypeError, match=msg): 341 df.loc[100.0, :] = df.iloc[0] 342 343 with pytest.raises(TypeError, match=msg): 344 df.loc[100, :] = df.iloc[0] 345 346 # allow object conversion here 347 df = orig.copy() 348 df.loc["a", :] = df.iloc[0] 349 exp = orig.append(Series(df.iloc[0], name="a")) 350 tm.assert_frame_equal(df, exp) 351 tm.assert_index_equal(df.index, Index(orig.index.tolist() + ["a"])) 352 assert df.index.dtype == "object" 353 354 def test_partial_set_empty_frame(self): 355 356 # partially set with an empty object 357 # frame 358 df = DataFrame() 359 360 msg = "cannot set a frame with no defined columns" 361 362 with pytest.raises(ValueError, match=msg): 363 df.loc[1] = 1 364 365 with pytest.raises(ValueError, match=msg): 366 df.loc[1] = Series([1], index=["foo"]) 367 368 msg = "cannot set a frame with no defined index and a scalar" 369 with pytest.raises(ValueError, match=msg): 370 df.loc[:, 1] = 1 371 372 # these work as they don't really change 373 # anything but the index 374 # GH5632 375 expected = DataFrame(columns=["foo"], index=Index([], dtype="object")) 376 377 def f(): 378 df = DataFrame(index=Index([], dtype="object")) 379 df["foo"] = Series([], dtype="object") 380 return df 381 382 tm.assert_frame_equal(f(), expected) 383 384 def f(): 385 df = DataFrame() 386 df["foo"] = Series(df.index) 387 return df 388 389 tm.assert_frame_equal(f(), expected) 390 391 def f(): 392 df = DataFrame() 393 df["foo"] = df.index 394 return df 395 396 tm.assert_frame_equal(f(), expected) 397 398 expected = DataFrame(columns=["foo"], index=Index([], dtype="int64")) 399 expected["foo"] = expected["foo"].astype("float64") 400 401 def f(): 402 df = DataFrame(index=Index([], dtype="int64")) 403 df["foo"] = [] 404 return df 405 406 tm.assert_frame_equal(f(), expected) 407 408 def f(): 409 df = DataFrame(index=Index([], dtype="int64")) 410 df["foo"] = Series(np.arange(len(df)), dtype="float64") 411 return df 412 413 tm.assert_frame_equal(f(), expected) 414 415 def f(): 416 df = DataFrame(index=Index([], dtype="int64")) 417 df["foo"] = range(len(df)) 418 return df 419 420 expected = DataFrame(columns=["foo"], index=Index([], dtype="int64")) 421 expected["foo"] = expected["foo"].astype("float64") 422 tm.assert_frame_equal(f(), expected) 423 424 df = DataFrame() 425 tm.assert_index_equal(df.columns, Index([], dtype=object)) 426 df2 = DataFrame() 427 df2[1] = Series([1], index=["foo"]) 428 df.loc[:, 1] = Series([1], index=["foo"]) 429 tm.assert_frame_equal(df, DataFrame([[1]], index=["foo"], columns=[1])) 430 tm.assert_frame_equal(df, df2) 431 432 # no index to start 433 expected = DataFrame({0: Series(1, index=range(4))}, columns=["A", "B", 0]) 434 435 df = DataFrame(columns=["A", "B"]) 436 df[0] = Series(1, index=range(4)) 437 df.dtypes 438 str(df) 439 tm.assert_frame_equal(df, expected) 440 441 df = DataFrame(columns=["A", "B"]) 442 df.loc[:, 0] = Series(1, index=range(4)) 443 df.dtypes 444 str(df) 445 tm.assert_frame_equal(df, expected) 446 447 def test_partial_set_empty_frame_row(self): 448 # GH5720, GH5744 449 # don't create rows when empty 450 expected = DataFrame(columns=["A", "B", "New"], index=Index([], dtype="int64")) 451 expected["A"] = expected["A"].astype("int64") 452 expected["B"] = expected["B"].astype("float64") 453 expected["New"] = expected["New"].astype("float64") 454 455 df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) 456 y = df[df.A > 5] 457 y["New"] = np.nan 458 tm.assert_frame_equal(y, expected) 459 # tm.assert_frame_equal(y,expected) 460 461 expected = DataFrame(columns=["a", "b", "c c", "d"]) 462 expected["d"] = expected["d"].astype("int64") 463 df = DataFrame(columns=["a", "b", "c c"]) 464 df["d"] = 3 465 tm.assert_frame_equal(df, expected) 466 tm.assert_series_equal(df["c c"], Series(name="c c", dtype=object)) 467 468 # reindex columns is ok 469 df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) 470 y = df[df.A > 5] 471 result = y.reindex(columns=["A", "B", "C"]) 472 expected = DataFrame(columns=["A", "B", "C"], index=Index([], dtype="int64")) 473 expected["A"] = expected["A"].astype("int64") 474 expected["B"] = expected["B"].astype("float64") 475 expected["C"] = expected["C"].astype("float64") 476 tm.assert_frame_equal(result, expected) 477 478 def test_partial_set_empty_frame_set_series(self): 479 # GH 5756 480 # setting with empty Series 481 df = DataFrame(Series(dtype=object)) 482 expected = DataFrame({0: Series(dtype=object)}) 483 tm.assert_frame_equal(df, expected) 484 485 df = DataFrame(Series(name="foo", dtype=object)) 486 expected = DataFrame({"foo": Series(dtype=object)}) 487 tm.assert_frame_equal(df, expected) 488 489 def test_partial_set_empty_frame_empty_copy_assignment(self): 490 # GH 5932 491 # copy on empty with assignment fails 492 df = DataFrame(index=[0]) 493 df = df.copy() 494 df["a"] = 0 495 expected = DataFrame(0, index=[0], columns=["a"]) 496 tm.assert_frame_equal(df, expected) 497 498 def test_partial_set_empty_frame_empty_consistencies(self): 499 # GH 6171 500 # consistency on empty frames 501 df = DataFrame(columns=["x", "y"]) 502 df["x"] = [1, 2] 503 expected = DataFrame({"x": [1, 2], "y": [np.nan, np.nan]}) 504 tm.assert_frame_equal(df, expected, check_dtype=False) 505 506 df = DataFrame(columns=["x", "y"]) 507 df["x"] = ["1", "2"] 508 expected = DataFrame({"x": ["1", "2"], "y": [np.nan, np.nan]}, dtype=object) 509 tm.assert_frame_equal(df, expected) 510 511 df = DataFrame(columns=["x", "y"]) 512 df.loc[0, "x"] = 1 513 expected = DataFrame({"x": [1], "y": [np.nan]}) 514 tm.assert_frame_equal(df, expected, check_dtype=False) 515 516 @pytest.mark.parametrize( 517 "idx,labels,expected_idx", 518 [ 519 ( 520 period_range(start="2000", periods=20, freq="D"), 521 ["2000-01-04", "2000-01-08", "2000-01-12"], 522 [ 523 Period("2000-01-04", freq="D"), 524 Period("2000-01-08", freq="D"), 525 Period("2000-01-12", freq="D"), 526 ], 527 ), 528 ( 529 date_range(start="2000", periods=20, freq="D"), 530 ["2000-01-04", "2000-01-08", "2000-01-12"], 531 [ 532 Timestamp("2000-01-04", freq="D"), 533 Timestamp("2000-01-08", freq="D"), 534 Timestamp("2000-01-12", freq="D"), 535 ], 536 ), 537 ( 538 pd.timedelta_range(start="1 day", periods=20), 539 ["4D", "8D", "12D"], 540 [pd.Timedelta("4 day"), pd.Timedelta("8 day"), pd.Timedelta("12 day")], 541 ), 542 ], 543 ) 544 def test_loc_with_list_of_strings_representing_datetimes( 545 self, idx, labels, expected_idx, frame_or_series 546 ): 547 # GH 11278 548 obj = frame_or_series(range(20), index=idx) 549 550 expected_value = [3, 7, 11] 551 expected = frame_or_series(expected_value, expected_idx) 552 553 tm.assert_equal(expected, obj.loc[labels]) 554 if frame_or_series is Series: 555 tm.assert_series_equal(expected, obj[labels]) 556 557 @pytest.mark.parametrize( 558 "idx,labels", 559 [ 560 ( 561 period_range(start="2000", periods=20, freq="D"), 562 ["2000-01-04", "2000-01-30"], 563 ), 564 ( 565 date_range(start="2000", periods=20, freq="D"), 566 ["2000-01-04", "2000-01-30"], 567 ), 568 (pd.timedelta_range(start="1 day", periods=20), ["3 day", "30 day"]), 569 ], 570 ) 571 def test_loc_with_list_of_strings_representing_datetimes_missing_value( 572 self, idx, labels 573 ): 574 # GH 11278 575 s = Series(range(20), index=idx) 576 df = DataFrame(range(20), index=idx) 577 msg = r"with any missing labels" 578 579 with pytest.raises(KeyError, match=msg): 580 s.loc[labels] 581 with pytest.raises(KeyError, match=msg): 582 s[labels] 583 with pytest.raises(KeyError, match=msg): 584 df.loc[labels] 585 586 @pytest.mark.parametrize( 587 "idx,labels,msg", 588 [ 589 ( 590 period_range(start="2000", periods=20, freq="D"), 591 ["4D", "8D"], 592 ( 593 r"None of \[Index\(\['4D', '8D'\], dtype='object'\)\] " 594 r"are in the \[index\]" 595 ), 596 ), 597 ( 598 date_range(start="2000", periods=20, freq="D"), 599 ["4D", "8D"], 600 ( 601 r"None of \[Index\(\['4D', '8D'\], dtype='object'\)\] " 602 r"are in the \[index\]" 603 ), 604 ), 605 ( 606 pd.timedelta_range(start="1 day", periods=20), 607 ["2000-01-04", "2000-01-08"], 608 ( 609 r"None of \[Index\(\['2000-01-04', '2000-01-08'\], " 610 r"dtype='object'\)\] are in the \[index\]" 611 ), 612 ), 613 ], 614 ) 615 def test_loc_with_list_of_strings_representing_datetimes_not_matched_type( 616 self, idx, labels, msg 617 ): 618 # GH 11278 619 s = Series(range(20), index=idx) 620 df = DataFrame(range(20), index=idx) 621 622 with pytest.raises(KeyError, match=msg): 623 s.loc[labels] 624 with pytest.raises(KeyError, match=msg): 625 s[labels] 626 with pytest.raises(KeyError, match=msg): 627 df.loc[labels] 628 629 def test_index_name_empty(self): 630 # GH 31368 631 df = DataFrame({}, index=pd.RangeIndex(0, name="df_index")) 632 series = Series(1.23, index=pd.RangeIndex(4, name="series_index")) 633 634 df["series"] = series 635 expected = DataFrame( 636 {"series": [1.23] * 4}, index=pd.RangeIndex(4, name="df_index") 637 ) 638 639 tm.assert_frame_equal(df, expected) 640 641 # GH 36527 642 df = DataFrame() 643 series = Series(1.23, index=pd.RangeIndex(4, name="series_index")) 644 df["series"] = series 645 expected = DataFrame( 646 {"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index") 647 ) 648 tm.assert_frame_equal(df, expected) 649 650 def test_slice_irregular_datetime_index_with_nan(self): 651 # GH36953 652 index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03", None]) 653 df = DataFrame(range(len(index)), index=index) 654 expected = DataFrame(range(len(index[:3])), index=index[:3]) 655 result = df["2012-01-01":"2012-01-04"] 656 tm.assert_frame_equal(result, expected) 657