|
5 | 5 | from warnings import catch_warnings
|
6 | 6 | import numpy as np
|
7 | 7 |
|
8 |
| -from pandas import DataFrame, Series, MultiIndex, Panel |
| 8 | +from pandas import DataFrame, Series, MultiIndex, Panel, Index |
9 | 9 | import pandas as pd
|
10 | 10 | import pandas.util.testing as tm
|
11 | 11 |
|
@@ -247,3 +247,270 @@ def test_subclass_sparse_transpose(self):
|
247 | 247 | [2, 5],
|
248 | 248 | [3, 6]])
|
249 | 249 | tm.assert_sp_frame_equal(ossdf.T, essdf)
|
| 250 | + |
| 251 | + def test_subclass_stack(self): |
| 252 | + # GH 15564 |
| 253 | + df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 254 | + index=['a', 'b', 'c'], |
| 255 | + columns=['X', 'Y', 'Z']) |
| 256 | + |
| 257 | + res = df.stack() |
| 258 | + exp = tm.SubclassedSeries( |
| 259 | + [1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 260 | + index=[list('aaabbbccc'), list('XYZXYZXYZ')]) |
| 261 | + |
| 262 | + tm.assert_series_equal(res, exp) |
| 263 | + |
| 264 | + def test_subclass_stack_multi(self): |
| 265 | + # GH 15564 |
| 266 | + df = tm.SubclassedDataFrame([ |
| 267 | + [10, 11, 12, 13], |
| 268 | + [20, 21, 22, 23], |
| 269 | + [30, 31, 32, 33], |
| 270 | + [40, 41, 42, 43]], |
| 271 | + index=MultiIndex.from_tuples( |
| 272 | + list(zip(list('AABB'), list('cdcd'))), |
| 273 | + names=['aaa', 'ccc']), |
| 274 | + columns=MultiIndex.from_tuples( |
| 275 | + list(zip(list('WWXX'), list('yzyz'))), |
| 276 | + names=['www', 'yyy'])) |
| 277 | + |
| 278 | + exp = tm.SubclassedDataFrame([ |
| 279 | + [10, 12], |
| 280 | + [11, 13], |
| 281 | + [20, 22], |
| 282 | + [21, 23], |
| 283 | + [30, 32], |
| 284 | + [31, 33], |
| 285 | + [40, 42], |
| 286 | + [41, 43]], |
| 287 | + index=MultiIndex.from_tuples(list(zip( |
| 288 | + list('AAAABBBB'), list('ccddccdd'), list('yzyzyzyz'))), |
| 289 | + names=['aaa', 'ccc', 'yyy']), |
| 290 | + columns=Index(['W', 'X'], name='www')) |
| 291 | + |
| 292 | + res = df.stack() |
| 293 | + tm.assert_frame_equal(res, exp) |
| 294 | + |
| 295 | + res = df.stack('yyy') |
| 296 | + tm.assert_frame_equal(res, exp) |
| 297 | + |
| 298 | + exp = tm.SubclassedDataFrame([ |
| 299 | + [10, 11], |
| 300 | + [12, 13], |
| 301 | + [20, 21], |
| 302 | + [22, 23], |
| 303 | + [30, 31], |
| 304 | + [32, 33], |
| 305 | + [40, 41], |
| 306 | + [42, 43]], |
| 307 | + index=MultiIndex.from_tuples(list(zip( |
| 308 | + list('AAAABBBB'), list('ccddccdd'), list('WXWXWXWX'))), |
| 309 | + names=['aaa', 'ccc', 'www']), |
| 310 | + columns=Index(['y', 'z'], name='yyy')) |
| 311 | + |
| 312 | + res = df.stack('www') |
| 313 | + tm.assert_frame_equal(res, exp) |
| 314 | + |
| 315 | + def test_subclass_stack_multi_mixed(self): |
| 316 | + # GH 15564 |
| 317 | + df = tm.SubclassedDataFrame([ |
| 318 | + [10, 11, 12.0, 13.0], |
| 319 | + [20, 21, 22.0, 23.0], |
| 320 | + [30, 31, 32.0, 33.0], |
| 321 | + [40, 41, 42.0, 43.0]], |
| 322 | + index=MultiIndex.from_tuples( |
| 323 | + list(zip(list('AABB'), list('cdcd'))), |
| 324 | + names=['aaa', 'ccc']), |
| 325 | + columns=MultiIndex.from_tuples( |
| 326 | + list(zip(list('WWXX'), list('yzyz'))), |
| 327 | + names=['www', 'yyy'])) |
| 328 | + |
| 329 | + exp = tm.SubclassedDataFrame([ |
| 330 | + [10, 12.0], |
| 331 | + [11, 13.0], |
| 332 | + [20, 22.0], |
| 333 | + [21, 23.0], |
| 334 | + [30, 32.0], |
| 335 | + [31, 33.0], |
| 336 | + [40, 42.0], |
| 337 | + [41, 43.0]], |
| 338 | + index=MultiIndex.from_tuples(list(zip( |
| 339 | + list('AAAABBBB'), list('ccddccdd'), list('yzyzyzyz'))), |
| 340 | + names=['aaa', 'ccc', 'yyy']), |
| 341 | + columns=Index(['W', 'X'], name='www')) |
| 342 | + |
| 343 | + res = df.stack() |
| 344 | + tm.assert_frame_equal(res, exp) |
| 345 | + |
| 346 | + res = df.stack('yyy') |
| 347 | + tm.assert_frame_equal(res, exp) |
| 348 | + |
| 349 | + exp = tm.SubclassedDataFrame([ |
| 350 | + [10.0, 11.0], |
| 351 | + [12.0, 13.0], |
| 352 | + [20.0, 21.0], |
| 353 | + [22.0, 23.0], |
| 354 | + [30.0, 31.0], |
| 355 | + [32.0, 33.0], |
| 356 | + [40.0, 41.0], |
| 357 | + [42.0, 43.0]], |
| 358 | + index=MultiIndex.from_tuples(list(zip( |
| 359 | + list('AAAABBBB'), list('ccddccdd'), list('WXWXWXWX'))), |
| 360 | + names=['aaa', 'ccc', 'www']), |
| 361 | + columns=Index(['y', 'z'], name='yyy')) |
| 362 | + |
| 363 | + res = df.stack('www') |
| 364 | + tm.assert_frame_equal(res, exp) |
| 365 | + |
| 366 | + def test_subclass_unstack(self): |
| 367 | + # GH 15564 |
| 368 | + df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 369 | + index=['a', 'b', 'c'], |
| 370 | + columns=['X', 'Y', 'Z']) |
| 371 | + |
| 372 | + res = df.unstack() |
| 373 | + exp = tm.SubclassedSeries( |
| 374 | + [1, 4, 7, 2, 5, 8, 3, 6, 9], |
| 375 | + index=[list('XXXYYYZZZ'), list('abcabcabc')]) |
| 376 | + |
| 377 | + tm.assert_series_equal(res, exp) |
| 378 | + |
| 379 | + def test_subclass_unstack_multi(self): |
| 380 | + # GH 15564 |
| 381 | + df = tm.SubclassedDataFrame([ |
| 382 | + [10, 11, 12, 13], |
| 383 | + [20, 21, 22, 23], |
| 384 | + [30, 31, 32, 33], |
| 385 | + [40, 41, 42, 43]], |
| 386 | + index=MultiIndex.from_tuples( |
| 387 | + list(zip(list('AABB'), list('cdcd'))), |
| 388 | + names=['aaa', 'ccc']), |
| 389 | + columns=MultiIndex.from_tuples( |
| 390 | + list(zip(list('WWXX'), list('yzyz'))), |
| 391 | + names=['www', 'yyy'])) |
| 392 | + |
| 393 | + exp = tm.SubclassedDataFrame([ |
| 394 | + [10, 20, 11, 21, 12, 22, 13, 23], |
| 395 | + [30, 40, 31, 41, 32, 42, 33, 43]], |
| 396 | + index=Index(['A', 'B'], name='aaa'), |
| 397 | + columns=MultiIndex.from_tuples(list(zip( |
| 398 | + list('WWWWXXXX'), list('yyzzyyzz'), list('cdcdcdcd'))), |
| 399 | + names=['www', 'yyy', 'ccc'])) |
| 400 | + |
| 401 | + res = df.unstack() |
| 402 | + tm.assert_frame_equal(res, exp) |
| 403 | + |
| 404 | + res = df.unstack('ccc') |
| 405 | + tm.assert_frame_equal(res, exp) |
| 406 | + |
| 407 | + exp = tm.SubclassedDataFrame([ |
| 408 | + [10, 30, 11, 31, 12, 32, 13, 33], |
| 409 | + [20, 40, 21, 41, 22, 42, 23, 43]], |
| 410 | + index=Index(['c', 'd'], name='ccc'), |
| 411 | + columns=MultiIndex.from_tuples(list(zip( |
| 412 | + list('WWWWXXXX'), list('yyzzyyzz'), list('ABABABAB'))), |
| 413 | + names=['www', 'yyy', 'aaa'])) |
| 414 | + |
| 415 | + res = df.unstack('aaa') |
| 416 | + tm.assert_frame_equal(res, exp) |
| 417 | + |
| 418 | + def test_subclass_unstack_multi_mixed(self): |
| 419 | + # GH 15564 |
| 420 | + df = tm.SubclassedDataFrame([ |
| 421 | + [10, 11, 12.0, 13.0], |
| 422 | + [20, 21, 22.0, 23.0], |
| 423 | + [30, 31, 32.0, 33.0], |
| 424 | + [40, 41, 42.0, 43.0]], |
| 425 | + index=MultiIndex.from_tuples( |
| 426 | + list(zip(list('AABB'), list('cdcd'))), |
| 427 | + names=['aaa', 'ccc']), |
| 428 | + columns=MultiIndex.from_tuples( |
| 429 | + list(zip(list('WWXX'), list('yzyz'))), |
| 430 | + names=['www', 'yyy'])) |
| 431 | + |
| 432 | + exp = tm.SubclassedDataFrame([ |
| 433 | + [10, 20, 11, 21, 12.0, 22.0, 13.0, 23.0], |
| 434 | + [30, 40, 31, 41, 32.0, 42.0, 33.0, 43.0]], |
| 435 | + index=Index(['A', 'B'], name='aaa'), |
| 436 | + columns=MultiIndex.from_tuples(list(zip( |
| 437 | + list('WWWWXXXX'), list('yyzzyyzz'), list('cdcdcdcd'))), |
| 438 | + names=['www', 'yyy', 'ccc'])) |
| 439 | + |
| 440 | + res = df.unstack() |
| 441 | + tm.assert_frame_equal(res, exp) |
| 442 | + |
| 443 | + res = df.unstack('ccc') |
| 444 | + tm.assert_frame_equal(res, exp) |
| 445 | + |
| 446 | + exp = tm.SubclassedDataFrame([ |
| 447 | + [10, 30, 11, 31, 12.0, 32.0, 13.0, 33.0], |
| 448 | + [20, 40, 21, 41, 22.0, 42.0, 23.0, 43.0]], |
| 449 | + index=Index(['c', 'd'], name='ccc'), |
| 450 | + columns=MultiIndex.from_tuples(list(zip( |
| 451 | + list('WWWWXXXX'), list('yyzzyyzz'), list('ABABABAB'))), |
| 452 | + names=['www', 'yyy', 'aaa'])) |
| 453 | + |
| 454 | + res = df.unstack('aaa') |
| 455 | + tm.assert_frame_equal(res, exp) |
| 456 | + |
| 457 | + def test_subclass_pivot(self): |
| 458 | + # GH 15564 |
| 459 | + df = tm.SubclassedDataFrame({ |
| 460 | + 'index': ['A', 'B', 'C', 'C', 'B', 'A'], |
| 461 | + 'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'], |
| 462 | + 'values': [1., 2., 3., 3., 2., 1.]}) |
| 463 | + |
| 464 | + pivoted = df.pivot( |
| 465 | + index='index', columns='columns', values='values') |
| 466 | + |
| 467 | + expected = tm.SubclassedDataFrame({ |
| 468 | + 'One': {'A': 1., 'B': 2., 'C': 3.}, |
| 469 | + 'Two': {'A': 1., 'B': 2., 'C': 3.}}) |
| 470 | + |
| 471 | + expected.index.name, expected.columns.name = 'index', 'columns' |
| 472 | + |
| 473 | + tm.assert_frame_equal(pivoted, expected) |
| 474 | + |
| 475 | + def test_subclassed_melt(self): |
| 476 | + # GH 15564 |
| 477 | + cheese = tm.SubclassedDataFrame({ |
| 478 | + 'first': ['John', 'Mary'], |
| 479 | + 'last': ['Doe', 'Bo'], |
| 480 | + 'height': [5.5, 6.0], |
| 481 | + 'weight': [130, 150]}) |
| 482 | + |
| 483 | + melted = pd.melt(cheese, id_vars=['first', 'last']) |
| 484 | + |
| 485 | + expected = tm.SubclassedDataFrame([ |
| 486 | + ['John', 'Doe', 'height', 5.5], |
| 487 | + ['Mary', 'Bo', 'height', 6.0], |
| 488 | + ['John', 'Doe', 'weight', 130], |
| 489 | + ['Mary', 'Bo', 'weight', 150]], |
| 490 | + columns=['first', 'last', 'variable', 'value']) |
| 491 | + |
| 492 | + tm.assert_frame_equal(melted, expected) |
| 493 | + |
| 494 | + def test_subclassed_wide_to_long(self): |
| 495 | + # GH 9762 |
| 496 | + |
| 497 | + np.random.seed(123) |
| 498 | + x = np.random.randn(3) |
| 499 | + df = tm.SubclassedDataFrame({ |
| 500 | + "A1970": {0: "a", 1: "b", 2: "c"}, |
| 501 | + "A1980": {0: "d", 1: "e", 2: "f"}, |
| 502 | + "B1970": {0: 2.5, 1: 1.2, 2: .7}, |
| 503 | + "B1980": {0: 3.2, 1: 1.3, 2: .1}, |
| 504 | + "X": dict(zip(range(3), x))}) |
| 505 | + |
| 506 | + df["id"] = df.index |
| 507 | + exp_data = {"X": x.tolist() + x.tolist(), |
| 508 | + "A": ['a', 'b', 'c', 'd', 'e', 'f'], |
| 509 | + "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1], |
| 510 | + "year": [1970, 1970, 1970, 1980, 1980, 1980], |
| 511 | + "id": [0, 1, 2, 0, 1, 2]} |
| 512 | + expected = tm.SubclassedDataFrame(exp_data) |
| 513 | + expected = expected.set_index(['id', 'year'])[["X", "A", "B"]] |
| 514 | + long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year") |
| 515 | + |
| 516 | + tm.assert_frame_equal(long_frame, expected) |
0 commit comments