-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathto_string_test.py
182 lines (153 loc) · 5.64 KB
/
to_string_test.py
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
from __future__ import annotations
from datetime import datetime
from typing import Any
import pytest
import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
from tests.utils import is_windows
data = {
"a": [
datetime(2021, 3, 1, 12, 34, 56, 49000),
datetime(2020, 1, 2, 2, 4, 14, 715000),
],
}
@pytest.mark.parametrize(
"fmt",
[
"%Y-%m-%d",
"%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%G-W%V-%u",
"%G-W%V",
],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_series(constructor_eager: ConstructorEager, fmt: str) -> None:
input_frame = nw.from_native(constructor_eager(data), eager_only=True)
input_series = input_frame["a"]
expected_col = [datetime.strftime(d, fmt) for d in data["a"]]
result = {"a": input_series.dt.to_string(fmt)}
if any(
x in str(constructor_eager) for x in ["pandas_pyarrow", "pyarrow_table", "modin"]
):
# PyArrow differs from other libraries, in that %S also shows
# the fraction of a second.
result = {"a": input_series.dt.to_string(fmt).str.replace(r"\.\d+$", "")}
assert_equal_data(result, {"a": expected_col})
@pytest.mark.parametrize(
"fmt",
[
"%Y-%m-%d",
"%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%G-W%V-%u",
"%G-W%V",
],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_expr(constructor: Constructor, fmt: str) -> None:
input_frame = nw.from_native(constructor(data))
expected_col = [datetime.strftime(d, fmt) for d in data["a"]]
result = input_frame.select(nw.col("a").dt.to_string(fmt).alias("b"))
if any(x in str(constructor) for x in ["pandas_pyarrow", "pyarrow_table", "modin"]):
# PyArrow differs from other libraries, in that %S also shows
# the fraction of a second.
result = input_frame.select(
nw.col("a").dt.to_string(fmt).str.replace(r"\.\d+$", "").alias("b")
)
assert_equal_data(result, {"b": expected_col})
def _clean_string(result: str) -> str:
# rstrip '0' to remove trailing zeros, as different libraries handle this differently
# if there's then a trailing `.`, remove that too.
if "." in result:
result = result.rstrip("0").rstrip(".")
return result
def _clean_string_expr(e: Any) -> Any:
# Same as `_clean_string` but for Expr
return e.str.replace_all(r"0+$", "").str.replace_all(r"\.$", "")
@pytest.mark.parametrize(
("data", "expected"),
[
(datetime(2020, 1, 9), "2020-01-09T00:00:00.000000"),
(datetime(2020, 1, 9, 12, 34, 56), "2020-01-09T12:34:56.000000"),
(datetime(2020, 1, 9, 12, 34, 56, 123), "2020-01-09T12:34:56.000123"),
(
datetime(2020, 1, 9, 12, 34, 56, 123456),
"2020-01-09T12:34:56.123456",
),
],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_iso_local_datetime_series(
constructor_eager: ConstructorEager, data: datetime, expected: str
) -> None:
df = constructor_eager({"a": [data]})
result = (
nw.from_native(df, eager_only=True)["a"]
.dt.to_string("%Y-%m-%dT%H:%M:%S.%f")
.item(0)
)
assert _clean_string(str(result)) == _clean_string(expected)
result = (
nw.from_native(df, eager_only=True)["a"]
.dt.to_string("%Y-%m-%dT%H:%M:%S%.f")
.item(0)
)
assert _clean_string(str(result)) == _clean_string(expected)
@pytest.mark.parametrize(
("data", "expected"),
[
(datetime(2020, 1, 9, 12, 34, 56), "2020-01-09T12:34:56.000000"),
(datetime(2020, 1, 9, 12, 34, 56, 123), "2020-01-09T12:34:56.000123"),
(
datetime(2020, 1, 9, 12, 34, 56, 123456),
"2020-01-09T12:34:56.123456",
),
],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_iso_local_datetime_expr(
constructor: Constructor,
data: datetime,
expected: str,
request: pytest.FixtureRequest,
) -> None:
if "duckdb" in str(constructor):
request.applymarker(pytest.mark.xfail)
df = constructor({"a": [data]})
result = nw.from_native(df).with_columns(
_clean_string_expr(nw.col("a").dt.to_string("%Y-%m-%dT%H:%M:%S.%f")).alias("b")
)
assert_equal_data(result, {"a": [data], "b": [_clean_string(expected)]})
result = nw.from_native(df).with_columns(
_clean_string_expr(nw.col("a").dt.to_string("%Y-%m-%dT%H:%M:%S%.f")).alias("b")
)
assert_equal_data(result, {"a": [data], "b": [_clean_string(expected)]})
@pytest.mark.parametrize(
("data", "expected"),
[(datetime(2020, 1, 9), "2020-01-09")],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_iso_local_date_series(
constructor_eager: ConstructorEager, data: datetime, expected: str
) -> None:
df = constructor_eager({"a": [data]})
result = nw.from_native(df, eager_only=True)["a"].dt.to_string("%Y-%m-%d").item(0)
assert str(result) == expected
@pytest.mark.parametrize(
("data", "expected"),
[(datetime(2020, 1, 9), "2020-01-09")],
)
@pytest.mark.skipif(is_windows(), reason="pyarrow breaking on windows")
def test_dt_to_string_iso_local_date_expr(
constructor: Constructor,
data: datetime,
expected: str,
) -> None:
df = constructor({"a": [data]})
result = nw.from_native(df).with_columns(
nw.col("a").dt.to_string("%Y-%m-%d").alias("b")
)
assert_equal_data(result, {"a": [data], "b": [expected]})