zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import numpy as np
import pytest
 
import pandas.util._test_decorators as td
 
from pandas import (
    DataFrame,
    NaT,
    Series,
    Timestamp,
    date_range,
    period_range,
)
import pandas._testing as tm
 
 
class TestDataFrameValues:
    @td.skip_array_manager_invalid_test
    def test_values(self, float_frame, using_copy_on_write):
        if using_copy_on_write:
            with pytest.raises(ValueError, match="read-only"):
                float_frame.values[:, 0] = 5.0
            assert (float_frame.values[:, 0] != 5).all()
        else:
            float_frame.values[:, 0] = 5.0
            assert (float_frame.values[:, 0] == 5).all()
 
    def test_more_values(self, float_string_frame):
        values = float_string_frame.values
        assert values.shape[1] == len(float_string_frame.columns)
 
    def test_values_mixed_dtypes(self, float_frame, float_string_frame):
        frame = float_frame
        arr = frame.values
 
        frame_cols = frame.columns
        for i, row in enumerate(arr):
            for j, value in enumerate(row):
                col = frame_cols[j]
                if np.isnan(value):
                    assert np.isnan(frame[col][i])
                else:
                    assert value == frame[col][i]
 
        # mixed type
        arr = float_string_frame[["foo", "A"]].values
        assert arr[0, 0] == "bar"
 
        df = DataFrame({"complex": [1j, 2j, 3j], "real": [1, 2, 3]})
        arr = df.values
        assert arr[0, 0] == 1j
 
    def test_values_duplicates(self):
        df = DataFrame(
            [[1, 2, "a", "b"], [1, 2, "a", "b"]], columns=["one", "one", "two", "two"]
        )
 
        result = df.values
        expected = np.array([[1, 2, "a", "b"], [1, 2, "a", "b"]], dtype=object)
 
        tm.assert_numpy_array_equal(result, expected)
 
    def test_values_with_duplicate_columns(self):
        df = DataFrame([[1, 2.5], [3, 4.5]], index=[1, 2], columns=["x", "x"])
        result = df.values
        expected = np.array([[1, 2.5], [3, 4.5]])
        assert (result == expected).all().all()
 
    @pytest.mark.parametrize("constructor", [date_range, period_range])
    def test_values_casts_datetimelike_to_object(self, constructor):
        series = Series(constructor("2000-01-01", periods=10, freq="D"))
 
        expected = series.astype("object")
 
        df = DataFrame({"a": series, "b": np.random.randn(len(series))})
 
        result = df.values.squeeze()
        assert (result[:, 0] == expected.values).all()
 
        df = DataFrame({"a": series, "b": ["foo"] * len(series)})
 
        result = df.values.squeeze()
        assert (result[:, 0] == expected.values).all()
 
    def test_frame_values_with_tz(self):
        tz = "US/Central"
        df = DataFrame({"A": date_range("2000", periods=4, tz=tz)})
        result = df.values
        expected = np.array(
            [
                [Timestamp("2000-01-01", tz=tz)],
                [Timestamp("2000-01-02", tz=tz)],
                [Timestamp("2000-01-03", tz=tz)],
                [Timestamp("2000-01-04", tz=tz)],
            ]
        )
        tm.assert_numpy_array_equal(result, expected)
 
        # two columns, homogeneous
 
        df["B"] = df["A"]
        result = df.values
        expected = np.concatenate([expected, expected], axis=1)
        tm.assert_numpy_array_equal(result, expected)
 
        # three columns, heterogeneous
        est = "US/Eastern"
        df["C"] = df["A"].dt.tz_convert(est)
 
        new = np.array(
            [
                [Timestamp("2000-01-01T01:00:00", tz=est)],
                [Timestamp("2000-01-02T01:00:00", tz=est)],
                [Timestamp("2000-01-03T01:00:00", tz=est)],
                [Timestamp("2000-01-04T01:00:00", tz=est)],
            ]
        )
        expected = np.concatenate([expected, new], axis=1)
        result = df.values
        tm.assert_numpy_array_equal(result, expected)
 
    def test_interleave_with_tzaware(self, timezone_frame):
        # interleave with object
        result = timezone_frame.assign(D="foo").values
        expected = np.array(
            [
                [
                    Timestamp("2013-01-01 00:00:00"),
                    Timestamp("2013-01-02 00:00:00"),
                    Timestamp("2013-01-03 00:00:00"),
                ],
                [
                    Timestamp("2013-01-01 00:00:00-0500", tz="US/Eastern"),
                    NaT,
                    Timestamp("2013-01-03 00:00:00-0500", tz="US/Eastern"),
                ],
                [
                    Timestamp("2013-01-01 00:00:00+0100", tz="CET"),
                    NaT,
                    Timestamp("2013-01-03 00:00:00+0100", tz="CET"),
                ],
                ["foo", "foo", "foo"],
            ],
            dtype=object,
        ).T
        tm.assert_numpy_array_equal(result, expected)
 
        # interleave with only datetime64[ns]
        result = timezone_frame.values
        expected = np.array(
            [
                [
                    Timestamp("2013-01-01 00:00:00"),
                    Timestamp("2013-01-02 00:00:00"),
                    Timestamp("2013-01-03 00:00:00"),
                ],
                [
                    Timestamp("2013-01-01 00:00:00-0500", tz="US/Eastern"),
                    NaT,
                    Timestamp("2013-01-03 00:00:00-0500", tz="US/Eastern"),
                ],
                [
                    Timestamp("2013-01-01 00:00:00+0100", tz="CET"),
                    NaT,
                    Timestamp("2013-01-03 00:00:00+0100", tz="CET"),
                ],
            ],
            dtype=object,
        ).T
        tm.assert_numpy_array_equal(result, expected)
 
    def test_values_interleave_non_unique_cols(self):
        df = DataFrame(
            [[Timestamp("20130101"), 3.5], [Timestamp("20130102"), 4.5]],
            columns=["x", "x"],
            index=[1, 2],
        )
 
        df_unique = df.copy()
        df_unique.columns = ["x", "y"]
        assert df_unique.values.shape == df.values.shape
        tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
        tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])
 
    def test_values_numeric_cols(self, float_frame):
        float_frame["foo"] = "bar"
 
        values = float_frame[["A", "B", "C", "D"]].values
        assert values.dtype == np.float64
 
    def test_values_lcd(self, mixed_float_frame, mixed_int_frame):
        # mixed lcd
        values = mixed_float_frame[["A", "B", "C", "D"]].values
        assert values.dtype == np.float64
 
        values = mixed_float_frame[["A", "B", "C"]].values
        assert values.dtype == np.float32
 
        values = mixed_float_frame[["C"]].values
        assert values.dtype == np.float16
 
        # GH#10364
        # B uint64 forces float because there are other signed int types
        values = mixed_int_frame[["A", "B", "C", "D"]].values
        assert values.dtype == np.float64
 
        values = mixed_int_frame[["A", "D"]].values
        assert values.dtype == np.int64
 
        # B uint64 forces float because there are other signed int types
        values = mixed_int_frame[["A", "B", "C"]].values
        assert values.dtype == np.float64
 
        # as B and C are both unsigned, no forcing to float is needed
        values = mixed_int_frame[["B", "C"]].values
        assert values.dtype == np.uint64
 
        values = mixed_int_frame[["A", "C"]].values
        assert values.dtype == np.int32
 
        values = mixed_int_frame[["C", "D"]].values
        assert values.dtype == np.int64
 
        values = mixed_int_frame[["A"]].values
        assert values.dtype == np.int32
 
        values = mixed_int_frame[["C"]].values
        assert values.dtype == np.uint8
 
 
class TestPrivateValues:
    @td.skip_array_manager_invalid_test
    def test_private_values_dt64tz(self, using_copy_on_write):
        dta = date_range("2000", periods=4, tz="US/Central")._data.reshape(-1, 1)
 
        df = DataFrame(dta, columns=["A"])
        tm.assert_equal(df._values, dta)
 
        if using_copy_on_write:
            assert not np.shares_memory(df._values._ndarray, dta._ndarray)
        else:
            # we have a view
            assert np.shares_memory(df._values._ndarray, dta._ndarray)
 
        # TimedeltaArray
        tda = dta - dta
        df2 = df - df
        tm.assert_equal(df2._values, tda)
 
    @td.skip_array_manager_invalid_test
    def test_private_values_dt64tz_multicol(self, using_copy_on_write):
        dta = date_range("2000", periods=8, tz="US/Central")._data.reshape(-1, 2)
 
        df = DataFrame(dta, columns=["A", "B"])
        tm.assert_equal(df._values, dta)
 
        if using_copy_on_write:
            assert not np.shares_memory(df._values._ndarray, dta._ndarray)
        else:
            # we have a view
            assert np.shares_memory(df._values._ndarray, dta._ndarray)
 
        # TimedeltaArray
        tda = dta - dta
        df2 = df - df
        tm.assert_equal(df2._values, tda)
 
    def test_private_values_dt64_multiblock(self):
        dta = date_range("2000", periods=8)._data
 
        df = DataFrame({"A": dta[:4]}, copy=False)
        df["B"] = dta[4:]
 
        assert len(df._mgr.arrays) == 2
 
        result = df._values
        expected = dta.reshape(2, 4).T
        tm.assert_equal(result, expected)