~teamproject/sisepeu/main

« back to all changes in this revision

Viewing changes to env/lib/python3.6/site-packages/pandas/tests/indexes/multi/test_conversion.py

  • Committer: riveramarlon113 at gmail
  • Date: 2023-06-04 02:19:28 UTC
  • Revision ID: riveramarlon113@gmail.com-20230604021928-rbt05g3480tfhxxj
Correcion de cosas pequeñas

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import numpy as np
2
 
import pytest
3
 
 
4
 
import pandas as pd
5
 
from pandas import DataFrame, MultiIndex
6
 
import pandas._testing as tm
7
 
 
8
 
 
9
 
def test_to_numpy(idx):
10
 
    result = idx.to_numpy()
11
 
    exp = idx.values
12
 
    tm.assert_numpy_array_equal(result, exp)
13
 
 
14
 
 
15
 
def test_to_frame():
16
 
    tuples = [(1, "one"), (1, "two"), (2, "one"), (2, "two")]
17
 
 
18
 
    index = MultiIndex.from_tuples(tuples)
19
 
    result = index.to_frame(index=False)
20
 
    expected = DataFrame(tuples)
21
 
    tm.assert_frame_equal(result, expected)
22
 
 
23
 
    result = index.to_frame()
24
 
    expected.index = index
25
 
    tm.assert_frame_equal(result, expected)
26
 
 
27
 
    tuples = [(1, "one"), (1, "two"), (2, "one"), (2, "two")]
28
 
    index = MultiIndex.from_tuples(tuples, names=["first", "second"])
29
 
    result = index.to_frame(index=False)
30
 
    expected = DataFrame(tuples)
31
 
    expected.columns = ["first", "second"]
32
 
    tm.assert_frame_equal(result, expected)
33
 
 
34
 
    result = index.to_frame()
35
 
    expected.index = index
36
 
    tm.assert_frame_equal(result, expected)
37
 
 
38
 
    # See GH-22580
39
 
    index = MultiIndex.from_tuples(tuples)
40
 
    result = index.to_frame(index=False, name=["first", "second"])
41
 
    expected = DataFrame(tuples)
42
 
    expected.columns = ["first", "second"]
43
 
    tm.assert_frame_equal(result, expected)
44
 
 
45
 
    result = index.to_frame(name=["first", "second"])
46
 
    expected.index = index
47
 
    expected.columns = ["first", "second"]
48
 
    tm.assert_frame_equal(result, expected)
49
 
 
50
 
    msg = "'name' must be a list / sequence of column names."
51
 
    with pytest.raises(TypeError, match=msg):
52
 
        index.to_frame(name="first")
53
 
 
54
 
    msg = "'name' should have same length as number of levels on index."
55
 
    with pytest.raises(ValueError, match=msg):
56
 
        index.to_frame(name=["first"])
57
 
 
58
 
    # Tests for datetime index
59
 
    index = MultiIndex.from_product([range(5), pd.date_range("20130101", periods=3)])
60
 
    result = index.to_frame(index=False)
61
 
    expected = DataFrame(
62
 
        {
63
 
            0: np.repeat(np.arange(5, dtype="int64"), 3),
64
 
            1: np.tile(pd.date_range("20130101", periods=3), 5),
65
 
        }
66
 
    )
67
 
    tm.assert_frame_equal(result, expected)
68
 
 
69
 
    result = index.to_frame()
70
 
    expected.index = index
71
 
    tm.assert_frame_equal(result, expected)
72
 
 
73
 
    # See GH-22580
74
 
    result = index.to_frame(index=False, name=["first", "second"])
75
 
    expected = DataFrame(
76
 
        {
77
 
            "first": np.repeat(np.arange(5, dtype="int64"), 3),
78
 
            "second": np.tile(pd.date_range("20130101", periods=3), 5),
79
 
        }
80
 
    )
81
 
    tm.assert_frame_equal(result, expected)
82
 
 
83
 
    result = index.to_frame(name=["first", "second"])
84
 
    expected.index = index
85
 
    tm.assert_frame_equal(result, expected)
86
 
 
87
 
 
88
 
def test_to_frame_dtype_fidelity():
89
 
    # GH 22420
90
 
    mi = pd.MultiIndex.from_arrays(
91
 
        [
92
 
            pd.date_range("19910905", periods=6, tz="US/Eastern"),
93
 
            [1, 1, 1, 2, 2, 2],
94
 
            pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True),
95
 
            ["x", "x", "y", "z", "x", "y"],
96
 
        ],
97
 
        names=["dates", "a", "b", "c"],
98
 
    )
99
 
    original_dtypes = {name: mi.levels[i].dtype for i, name in enumerate(mi.names)}
100
 
 
101
 
    expected_df = pd.DataFrame(
102
 
        {
103
 
            "dates": pd.date_range("19910905", periods=6, tz="US/Eastern"),
104
 
            "a": [1, 1, 1, 2, 2, 2],
105
 
            "b": pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True),
106
 
            "c": ["x", "x", "y", "z", "x", "y"],
107
 
        }
108
 
    )
109
 
    df = mi.to_frame(index=False)
110
 
    df_dtypes = df.dtypes.to_dict()
111
 
 
112
 
    tm.assert_frame_equal(df, expected_df)
113
 
    assert original_dtypes == df_dtypes
114
 
 
115
 
 
116
 
def test_to_frame_resulting_column_order():
117
 
    # GH 22420
118
 
    expected = ["z", 0, "a"]
119
 
    mi = pd.MultiIndex.from_arrays(
120
 
        [["a", "b", "c"], ["x", "y", "z"], ["q", "w", "e"]], names=expected
121
 
    )
122
 
    result = mi.to_frame().columns.tolist()
123
 
    assert result == expected
124
 
 
125
 
 
126
 
def test_to_flat_index(idx):
127
 
    expected = pd.Index(
128
 
        (
129
 
            ("foo", "one"),
130
 
            ("foo", "two"),
131
 
            ("bar", "one"),
132
 
            ("baz", "two"),
133
 
            ("qux", "one"),
134
 
            ("qux", "two"),
135
 
        ),
136
 
        tupleize_cols=False,
137
 
    )
138
 
    result = idx.to_flat_index()
139
 
    tm.assert_index_equal(result, expected)