This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdatetimes.py
More file actions
134 lines (116 loc) · 4.46 KB
/
datetimes.py
File metadata and controls
134 lines (116 loc) · 4.46 KB
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
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from collections.abc import Mapping
from datetime import date, datetime
from typing import Optional, TYPE_CHECKING, Union
import bigframes_vendored.constants as constants
import bigframes_vendored.pandas.core.tools.datetimes as vendored_pandas_datetimes
import pandas as pd
import bigframes.dataframe
import bigframes.dtypes
import bigframes.operations as ops
import bigframes.series
if TYPE_CHECKING:
import bigframes.session
def to_datetime(
arg: Union[
Union[int, float, str, datetime, date],
vendored_pandas_datetimes.local_iterables,
bigframes.series.Series,
bigframes.dataframe.DataFrame,
],
*,
utc: bool = False,
format: Optional[str] = None,
unit: Optional[str] = None,
session: Optional[bigframes.session.Session] = None,
) -> Union[pd.Timestamp, datetime, bigframes.series.Series]:
if isinstance(arg, (int, float, str, datetime, date)):
return pd.to_datetime(
arg,
utc=utc,
format=format,
unit=unit,
)
if isinstance(arg, (Mapping, pd.DataFrame, bigframes.dataframe.DataFrame)):
raise NotImplementedError(
"Conversion of Mapping, pandas.DataFrame, or bigframes.dataframe.DataFrame "
f"to datetime is not implemented. {constants.FEEDBACK_LINK}"
)
arg = bigframes.series.Series(arg, session=session)
if format and unit and arg.dtype in (bigframes.dtypes.INT_DTYPE, bigframes.dtypes.FLOAT_DTYPE): # type: ignore
raise ValueError("cannot specify both format and unit")
if unit and arg.dtype not in (bigframes.dtypes.INT_DTYPE, bigframes.dtypes.FLOAT_DTYPE): # type: ignore
raise NotImplementedError(
f"Unit parameter is not supported for non-numerical input types. {constants.FEEDBACK_LINK}"
)
if arg.dtype in (
bigframes.dtypes.TIMESTAMP_DTYPE,
bigframes.dtypes.DATETIME_DTYPE,
bigframes.dtypes.DATE_DTYPE,
):
to_type = (
bigframes.dtypes.TIMESTAMP_DTYPE if utc else bigframes.dtypes.DATETIME_DTYPE
)
return arg._apply_unary_op(ops.AsTypeOp(to_type=to_type)) # type: ignore
if (not utc) and arg.dtype == bigframes.dtypes.STRING_DTYPE:
if format:
raise NotImplementedError(
f"Customized formats are not supported for string inputs when utc=False. Please set utc=True if possible. {constants.FEEDBACK_LINK}"
)
assert unit is None
# The following operations evaluate individual values to infer a format,
# so cache if needed.
arg = arg._cached(force=False)
as_datetime = arg._apply_unary_op( # type: ignore
ops.ToDatetimeOp(
format=format,
unit=unit,
)
)
failed_datetime_cast = arg.notnull() & as_datetime.isnull()
is_utc = arg._apply_unary_op(
ops.EndsWithOp(
pat=("Z", "-00:00", "+00:00", "-0000", "+0000", "-00", "+00")
)
)
# Cast to DATETIME shall succeed if all inputs are tz-naive.
if not failed_datetime_cast.any():
return as_datetime
if is_utc.all():
return arg._apply_unary_op( # type: ignore
ops.ToTimestampOp(
format=format,
unit=unit,
)
)
raise NotImplementedError(
f"Non-UTC string inputs are not supported when utc=False. Please set utc=True if possible. {constants.FEEDBACK_LINK}"
)
# If utc:
elif utc:
return arg._apply_unary_op( # type: ignore
ops.ToTimestampOp(
format=format,
unit=unit,
)
)
else:
return arg._apply_unary_op( # type: ignore
ops.ToDatetimeOp(
format=format,
unit=unit,
)
)