Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1632900, SNOW-1625232: Add support for Series.dt.normalize and DatetimeIndex.normalize #2143

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- Added support for `Series.dt.weekday`, `Series.dt.time`, and `DatetimeIndex.time`.
- Added support for `Index.min` and `Index.max`.
- Added support for `pd.merge_asof`.
- Added support for `Series.dt.normalize` and `DatetimeIndex.normalize`.

#### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions docs/source/modin/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ Series
Series.dt.floor
Series.dt.ceil
Series.dt.round
Series.dt.normalize


.. rubric:: String accessor methods
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modin/supported/datetime_index_supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Methods
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| DataFrame method | Snowpark implemented? (Y/N/P/D) | Missing parameters | Notes for current implementation |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| ``normalize`` | N | | |
| ``normalize`` | Y | | |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| ``strftime`` | N | | |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modin/supported/series_dt_supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ the method in the left column.
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``tz_convert`` | N | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``normalize`` | N | |
| ``normalize`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``strftime`` | N | |
+-----------------------------+---------------------------------+----------------------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16339,17 +16339,32 @@ def dt_floor(
)
)

def dt_normalize(self) -> None:
def dt_normalize(self, include_index: bool = False) -> "SnowflakeQueryCompiler":
"""
Set the time component of each date-time value to midnight.

Args:
include_index: Whether to include the index columns in the operation.

Returns
-------
BaseQueryCompiler
New QueryCompiler containing date-time values with midnight time.
"""
ErrorMessage.not_implemented(
"Snowpark pandas doesn't yet support the method 'Series.dt.normalize'"
internal_frame = self._modin_frame

def normalize_column(col_id: str) -> SnowparkColumn:
return builtin("date_trunc")("d", col(col_id))

snowflake_ids = internal_frame.data_column_snowflake_quoted_identifiers[0:1]
if include_index:
snowflake_ids.extend(
internal_frame.index_column_snowflake_quoted_identifiers
)
return SnowflakeQueryCompiler(
internal_frame.update_snowflake_quoted_identifiers_with_expressions(
{col_id: normalize_column(col_id) for col_id in snowflake_ids}
).frame
)

def dt_month_name(
Expand Down
40 changes: 39 additions & 1 deletion src/snowflake/snowpark/modin/plugin/docstrings/series_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,45 @@ def swapcase():
pass

def normalize():
pass
"""
Convert times to midnight.


The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases, when the time does not matter. Length is unaltered. The timezones are unaffected.


This method is available on Series with datetime values under the .dt accessor, and directly on Datetime Array/Index.


Returns
-------
DatetimeArray, DatetimeIndex or Series
The same type as the original data. Series will have the same name and index. DatetimeIndex will have the same name.

See also
--------
floor
Floor the datetimes to the specified freq.
ceil
Ceil the datetimes to the specified freq.
round
Round the datetimes to the specified freq.

Examples
--------
>>> idx = pd.date_range(start='2014-08-01 10:00', freq='h',
... periods=3, tz='Asia/Calcutta') # doctest: +SKIP
>>> idx # doctest: +SKIP
DatetimeIndex(['2014-08-01 10:00:00+05:30',
'2014-08-01 11:00:00+05:30',
'2014-08-01 12:00:00+05:30'],
dtype='datetime64[ns, Asia/Calcutta]', freq=None)
>>> idx.normalize() # doctest: +SKIP
DatetimeIndex(['2014-08-01 00:00:00+05:30',
'2014-08-01 00:00:00+05:30',
'2014-08-01 00:00:00+05:30'],
dtype='datetime64[ns, Asia/Calcutta]', freq=None)
"""

def translate():
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ def indexer_between_time(
array([0, 1])
"""

@datetime_index_not_implemented()
def normalize(self) -> DatetimeIndex:
"""
Convert times to midnight.
Expand Down Expand Up @@ -826,6 +825,9 @@ def normalize(self) -> DatetimeIndex:
'2014-08-01 00:00:00+05:30'],
dtype='datetime64[ns, Asia/Calcutta]', freq=None)
"""
return DatetimeIndex(
query_compiler=self._query_compiler.dt_normalize(include_index=True)
)

@datetime_index_not_implemented()
def strftime(self, date_format: str) -> np.ndarray[np.object_]:
Expand Down
12 changes: 12 additions & 0 deletions tests/integ/modin/index/test_datetime_index_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,15 @@ def test_day_month_name_negative(method):
msg = f"Snowpark pandas method DatetimeIndex.{method} does not yet support the 'locale' parameter"
with pytest.raises(NotImplementedError, match=msg):
getattr(snow_index, method)(locale="pt_BR.utf8")


@sql_count_checker(query_count=1)
def test_normalize():
native_index = native_pd.date_range(start="2021-01-01", periods=5, freq="7h")
native_index = native_index.append(native_pd.DatetimeIndex([pd.NaT]))
snow_index = pd.DatetimeIndex(native_index)
eval_snowpark_pandas_result(
snow_index,
native_index,
lambda i: i.normalize(),
)
13 changes: 13 additions & 0 deletions tests/integ/modin/series/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ def test_floor_ceil_round_negative(func, freq, ambiguous, nonexistent):
)


@sql_count_checker(query_count=1)
def test_normalize():
date_range = native_pd.date_range(start="2021-01-01", periods=5, freq="7h")
native_ser = native_pd.Series(date_range)
native_ser.iloc[2] = native_pd.NaT
snow_ser = pd.Series(native_ser)
eval_snowpark_pandas_result(
snow_ser,
native_ser,
lambda s: s.dt.normalize(),
)


def test_isocalendar():
with SqlCounter(query_count=1):
date_range = native_pd.date_range("2020-05-01", periods=5, freq="4D")
Expand Down
1 change: 0 additions & 1 deletion tests/unit/modin/test_series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def mock_query_compiler_for_dt_series() -> SnowflakeQueryCompiler:
(lambda s: s.dt.to_period(), "to_period"),
(lambda s: s.dt.tz_localize(tz="UTC"), "tz_localize"),
(lambda s: s.dt.tz_convert(tz="UTC"), "tz_convert"),
(lambda s: s.dt.normalize(), "normalize"),
(lambda s: s.dt.strftime(date_format="YY/MM/DD"), "strftime"),
(lambda s: s.dt.total_seconds(), "total_seconds"),
(lambda s: s.dt.seconds, "seconds"),
Expand Down
Loading