Skip to content

Commit 935e04b

Browse files
authored
fix(rust, python): make weekday tz-aware (#5989)
1 parent db0c741 commit 935e04b

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

polars/polars-core/src/chunked_array/temporal/datetime.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ impl DatetimeChunked {
4242
}
4343
}
4444

45-
#[cfg(feature = "timezones")]
46-
pub fn apply_tz_offset(&self, tz: &str) -> PolarsResult<DatetimeChunked> {
47-
let keep_tz = self.time_zone().as_deref().unwrap_or("UTC").to_string();
48-
self.clone()
49-
.with_time_zone(Some(tz.into()))
50-
.cast_time_zone(&keep_tz)
51-
}
52-
5345
pub fn apply_on_tz_corrected<F>(&self, mut func: F) -> PolarsResult<DatetimeChunked>
5446
where
5547
F: FnMut(DatetimeChunked) -> PolarsResult<DatetimeChunked>,

polars/polars-time/src/chunkedarray/datetime.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ pub trait DatetimeMethods: AsDatetime {
6969
/// Extract ISO weekday from underlying NaiveDateTime representation.
7070
/// Returns the weekday number where monday = 1 and sunday = 7
7171
fn weekday(&self) -> UInt32Chunked {
72-
let ca = self.as_datetime();
73-
let f = match ca.time_unit() {
74-
TimeUnit::Nanoseconds => datetime_to_weekday_ns,
75-
TimeUnit::Microseconds => datetime_to_weekday_us,
76-
TimeUnit::Milliseconds => datetime_to_weekday_ms,
77-
};
78-
ca.apply_kernel_cast::<UInt32Type>(&f)
72+
cast_and_apply(self.as_datetime(), temporal::weekday)
7973
}
8074

8175
/// Returns the ISO week number starting from 1.

polars/polars-time/src/chunkedarray/kernels.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,32 +174,6 @@ to_temporal_unit!(
174174
ArrowDataType::UInt32
175175
);
176176

177-
#[cfg(feature = "dtype-datetime")]
178-
to_temporal_unit!(
179-
datetime_to_weekday_ns,
180-
p_weekday,
181-
timestamp_ns_to_datetime,
182-
i64,
183-
ArrowDataType::UInt32
184-
);
185-
186-
#[cfg(feature = "dtype-datetime")]
187-
to_temporal_unit!(
188-
datetime_to_weekday_ms,
189-
p_weekday,
190-
timestamp_ms_to_datetime,
191-
i64,
192-
ArrowDataType::UInt32
193-
);
194-
#[cfg(feature = "dtype-datetime")]
195-
to_temporal_unit!(
196-
datetime_to_weekday_us,
197-
p_weekday,
198-
timestamp_us_to_datetime,
199-
i64,
200-
ArrowDataType::UInt32
201-
);
202-
203177
#[cfg(feature = "dtype-datetime")]
204178
to_temporal_unit!(
205179
datetime_to_iso_year_ns,

py-polars/tests/unit/test_datelike.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,3 +2306,34 @@ def test_cast_time_to_duration() -> None:
23062306
assert pl.Series([time(hour=0, minute=0, second=2)]).cast(
23072307
pl.Duration
23082308
).item() == timedelta(seconds=2)
2309+
2310+
2311+
def test_tz_aware_day_weekday() -> None:
2312+
start = datetime(2001, 1, 1)
2313+
stop = datetime(2001, 1, 9)
2314+
df = pl.DataFrame({"date": pl.date_range(start, stop, timedelta(days=3))})
2315+
2316+
df = df.with_columns(
2317+
[
2318+
pl.col("date").dt.with_time_zone("Asia/Tokyo").alias("tyo_date"),
2319+
pl.col("date").dt.with_time_zone("America/New_York").alias("ny_date"),
2320+
]
2321+
)
2322+
2323+
assert df.select(
2324+
[
2325+
pl.col("date").dt.day().alias("day"),
2326+
pl.col("tyo_date").dt.day().alias("tyo_day"),
2327+
pl.col("ny_date").dt.day().alias("ny_day"),
2328+
pl.col("date").dt.weekday().alias("weekday"),
2329+
pl.col("tyo_date").dt.weekday().alias("tyo_weekday"),
2330+
pl.col("ny_date").dt.weekday().alias("ny_weekday"),
2331+
]
2332+
).to_dict(False) == {
2333+
"day": [1, 4, 7],
2334+
"tyo_day": [1, 4, 7],
2335+
"ny_day": [31, 3, 6],
2336+
"weekday": [1, 4, 7],
2337+
"tyo_weekday": [1, 4, 7],
2338+
"ny_weekday": [7, 3, 6],
2339+
}

0 commit comments

Comments
 (0)