Class: Polars::DateTimeExpr
- Inherits:
-
Object
- Object
- Polars::DateTimeExpr
- Defined in:
- lib/polars/date_time_expr.rb
Overview
Namespace for datetime related expressions.
Instance Method Summary collapse
-
#cast_time_unit(tu) ⇒ Expr
Cast the underlying data to another time unit.
-
#cast_time_zone(tz) ⇒ Expr
Cast time zone for a Series of type Datetime.
-
#day ⇒ Expr
Extract day from underlying Date representation.
-
#days ⇒ Expr
Extract the days from a Duration type.
-
#epoch(tu = "us") ⇒ Expr
Get the time passed since the Unix EPOCH in the give time unit.
-
#hour ⇒ Expr
Extract hour from underlying DateTime representation.
-
#hours ⇒ Expr
Extract the hours from a Duration type.
-
#iso_year ⇒ Expr
Extract ISO year from underlying Date representation.
-
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
-
#microseconds ⇒ Expr
Extract the microseconds from a Duration type.
-
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
-
#milliseconds ⇒ Expr
Extract the milliseconds from a Duration type.
-
#minute ⇒ Expr
Extract minutes from underlying DateTime representation.
-
#minutes ⇒ Expr
Extract the minutes from a Duration type.
-
#month ⇒ Expr
Extract month from underlying Date representation.
-
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
-
#nanoseconds ⇒ Expr
Extract the nanoseconds from a Duration type.
-
#offset_by(by) ⇒ Expr
Offset this date by a relative time offset.
-
#ordinal_day ⇒ Expr
Extract ordinal day from underlying Date representation.
-
#quarter ⇒ Expr
Extract quarter from underlying Date representation.
-
#round(every, offset: nil) ⇒ Expr
Divide the date/datetime range into buckets.
-
#second(fractional: false) ⇒ Expr
Extract seconds from underlying DateTime representation.
-
#seconds ⇒ Expr
Extract the seconds from a Duration type.
-
#strftime(fmt) ⇒ Expr
Format Date/datetime with a formatting rule.
-
#timestamp(tu = "us") ⇒ Expr
Return a timestamp in the given time unit.
-
#truncate(every, offset: nil) ⇒ Expr
Divide the date/datetime range into buckets.
-
#tz_localize(tz) ⇒ Expr
Localize tz-naive Datetime Series to tz-aware Datetime Series.
-
#week ⇒ Expr
Extract the week from the underlying Date representation.
-
#weekday ⇒ Expr
Extract the week day from the underlying Date representation.
-
#with_time_unit(tu) ⇒ Expr
Set time unit of a Series of dtype Datetime or Duration.
-
#with_time_zone(tz) ⇒ Expr
Set time zone for a Series of type Datetime.
-
#year ⇒ Expr
Extract year from underlying Date representation.
Instance Method Details
#cast_time_unit(tu) ⇒ Expr
Cast the underlying data to another time unit. This may lose precision.
999 1000 1001 |
# File 'lib/polars/date_time_expr.rb', line 999 def cast_time_unit(tu) Utils.wrap_expr(_rbexpr.dt_cast_time_unit(tu)) end |
#cast_time_zone(tz) ⇒ Expr
Cast time zone for a Series of type Datetime.
Different from with_time_zone
, this will also modify
the underlying timestamp,
1052 1053 1054 |
# File 'lib/polars/date_time_expr.rb', line 1052 def cast_time_zone(tz) Utils.wrap_expr(_rbexpr.dt_cast_time_zone(tz)) end |
#day ⇒ Expr
Extract day from underlying Date representation.
Applies to Date and Datetime columns.
Returns the day of month starting from 1. The return value ranges from 1 to 31. (The last day of month differs by months.)
563 564 565 |
# File 'lib/polars/date_time_expr.rb', line 563 def day Utils.wrap_expr(_rbexpr.day) end |
#days ⇒ Expr
Extract the days from a Duration type.
1100 1101 1102 |
# File 'lib/polars/date_time_expr.rb', line 1100 def days Utils.wrap_expr(_rbexpr.duration_days) end |
#epoch(tu = "us") ⇒ Expr
Get the time passed since the Unix EPOCH in the give time unit.
876 877 878 879 880 881 882 883 884 885 886 |
# File 'lib/polars/date_time_expr.rb', line 876 def epoch(tu = "us") if Utils::DTYPE_TEMPORAL_UNITS.include?(tu) (tu) elsif tu == "s" Utils.wrap_expr(_rbexpr.dt_epoch_seconds) elsif tu == "d" Utils.wrap_expr(_rbexpr).cast(:date).cast(:i32) else raise ArgumentError, "tu must be one of {{'ns', 'us', 'ms', 's', 'd'}}, got #{tu}" end end |
#hour ⇒ Expr
Extract hour from underlying DateTime representation.
Applies to Datetime columns.
Returns the hour number from 0 to 23.
660 661 662 |
# File 'lib/polars/date_time_expr.rb', line 660 def hour Utils.wrap_expr(_rbexpr.hour) end |
#hours ⇒ Expr
Extract the hours from a Duration type.
1137 1138 1139 |
# File 'lib/polars/date_time_expr.rb', line 1137 def hours Utils.wrap_expr(_rbexpr.duration_hours) end |
#iso_year ⇒ Expr
Extract ISO year from underlying Date representation.
Applies to Date and Datetime columns.
Returns the year number in the ISO standard. This may not correspond with the calendar year.
323 324 325 |
# File 'lib/polars/date_time_expr.rb', line 323 def iso_year Utils.wrap_expr(_rbexpr.iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
832 833 834 |
# File 'lib/polars/date_time_expr.rb', line 832 def microsecond Utils.wrap_expr(_rbexpr.microsecond) end |
#microseconds ⇒ Expr
Extract the microseconds from a Duration type.
1307 1308 1309 |
# File 'lib/polars/date_time_expr.rb', line 1307 def microseconds Utils.wrap_expr(_rbexpr.duration_microseconds) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
823 824 825 |
# File 'lib/polars/date_time_expr.rb', line 823 def millisecond Utils.wrap_expr(_rbexpr.millisecond) end |
#milliseconds ⇒ Expr
Extract the milliseconds from a Duration type.
1260 1261 1262 |
# File 'lib/polars/date_time_expr.rb', line 1260 def milliseconds Utils.wrap_expr(_rbexpr.duration_milliseconds) end |
#minute ⇒ Expr
Extract minutes from underlying DateTime representation.
Applies to Datetime columns.
Returns the minute number from 0 to 59.
705 706 707 |
# File 'lib/polars/date_time_expr.rb', line 705 def minute Utils.wrap_expr(_rbexpr.minute) end |
#minutes ⇒ Expr
Extract the minutes from a Duration type.
1174 1175 1176 |
# File 'lib/polars/date_time_expr.rb', line 1174 def minutes Utils.wrap_expr(_rbexpr.duration_minutes) end |
#month ⇒ Expr
Extract month from underlying Date representation.
Applies to Date and Datetime columns.
Returns the month number starting from 1. The return value ranges from 1 to 12.
414 415 416 |
# File 'lib/polars/date_time_expr.rb', line 414 def month Utils.wrap_expr(_rbexpr.month) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
841 842 843 |
# File 'lib/polars/date_time_expr.rb', line 841 def nanosecond Utils.wrap_expr(_rbexpr.nanosecond) end |
#nanoseconds ⇒ Expr
Extract the nanoseconds from a Duration type.
1354 1355 1356 |
# File 'lib/polars/date_time_expr.rb', line 1354 def nanoseconds Utils.wrap_expr(_rbexpr.duration_nanoseconds) end |
#offset_by(by) ⇒ Expr
Offset this date by a relative time offset.
This differs from Polars.col("foo") + timedelta
in that it can
take months and leap years into account. Note that only a single minus
sign is allowed in the by
string, as the first character.
1414 1415 1416 |
# File 'lib/polars/date_time_expr.rb', line 1414 def offset_by(by) Utils.wrap_expr(_rbexpr.dt_offset_by(by)) end |
#ordinal_day ⇒ Expr
Extract ordinal day from underlying Date representation.
Applies to Date and Datetime columns.
Returns the day of month starting from 1. The return value ranges from 1 to 31. (The last day of month differs by months.)
615 616 617 |
# File 'lib/polars/date_time_expr.rb', line 615 def ordinal_day Utils.wrap_expr(_rbexpr.ordinal_day) end |
#quarter ⇒ Expr
Extract quarter from underlying Date representation.
Applies to Date and Datetime columns.
Returns the quarter ranging from 1 to 4.
368 369 370 |
# File 'lib/polars/date_time_expr.rb', line 368 def quarter Utils.wrap_expr(_rbexpr.quarter) end |
#round(every, offset: nil) ⇒ Expr
The every
and offset
argument are created with the
the following small string formatting language:
1ns # 1 nanosecond 1us # 1 microsecond 1ms # 1 millisecond 1s # 1 second 1m # 1 minute 1h # 1 hour 1d # 1 day 1w # 1 week 1mo # 1 calendar month 1y # 1 calendar year
eg: 3d12h4m25s # 3 days, 12 hours, 4 minutes, and 25 seconds
This functionality is currently experimental and may change without it being considered a breaking change.
Divide the date/datetime range into buckets.
Each date/datetime in the first half of the interval is mapped to the start of its bucket. Each date/datetime in the seconod half of the interval is mapped to the end of its bucket.
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/polars/date_time_expr.rb', line 244 def round(every, offset: nil) if offset.nil? offset = "0ns" end Utils.wrap_expr( _rbexpr.dt_round( Utils._timedelta_to_pl_duration(every), Utils._timedelta_to_pl_duration(offset) ) ) end |
#second(fractional: false) ⇒ Expr
Extract seconds from underlying DateTime representation.
Applies to Datetime columns.
Returns the integer second number from 0 to 59, or a floating
point number from 0 < 60 if fractional: true
that includes
any milli/micro/nanosecond component.
809 810 811 812 813 814 815 816 |
# File 'lib/polars/date_time_expr.rb', line 809 def second(fractional: false) sec = Utils.wrap_expr(_rbexpr.second) if fractional sec + (Utils.wrap_expr(_rbexpr.nanosecond) / Utils.lit(1_000_000_000.0)) else sec end end |
#seconds ⇒ Expr
Extract the seconds from a Duration type.
1213 1214 1215 |
# File 'lib/polars/date_time_expr.rb', line 1213 def seconds Utils.wrap_expr(_rbexpr.duration_seconds) end |
#strftime(fmt) ⇒ Expr
Format Date/datetime with a formatting rule.
262 263 264 |
# File 'lib/polars/date_time_expr.rb', line 262 def strftime(fmt) Utils.wrap_expr(_rbexpr.strftime(fmt)) end |
#timestamp(tu = "us") ⇒ Expr
Return a timestamp in the given time unit.
919 920 921 |
# File 'lib/polars/date_time_expr.rb', line 919 def (tu = "us") Utils.wrap_expr(_rbexpr.(tu)) end |
#truncate(every, offset: nil) ⇒ Expr
The every
and offset
argument are created with the
the following small string formatting language:
1ns # 1 nanosecond 1us # 1 microsecond 1ms # 1 millisecond 1s # 1 second 1m # 1 minute 1h # 1 hour 1d # 1 day 1w # 1 week 1mo # 1 calendar month 1y # 1 calendar year
eg: 3d12h4m25s # 3 days, 12 hours, 4 minutes, and 25 seconds
Divide the date/datetime range into buckets.
Each date/datetime is mapped to the start of its bucket.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/polars/date_time_expr.rb', line 118 def truncate(every, offset: nil) if offset.nil? offset = "0ns" end Utils.wrap_expr( _rbexpr.dt_truncate( Utils._timedelta_to_pl_duration(every), Utils._timedelta_to_pl_duration(offset) ) ) end |
#tz_localize(tz) ⇒ Expr
Localize tz-naive Datetime Series to tz-aware Datetime Series.
This method takes a naive Datetime Series and makes this time zone aware. It does not move the time to another time zone.
1065 1066 1067 |
# File 'lib/polars/date_time_expr.rb', line 1065 def tz_localize(tz) Utils.wrap_expr(_rbexpr.dt_tz_localize(tz)) end |
#week ⇒ Expr
Extract the week from the underlying Date representation.
Applies to Date and Datetime columns.
Returns the ISO week number starting from 1. The return value ranges from 1 to 53. (The last week of year differs by years.)
460 461 462 |
# File 'lib/polars/date_time_expr.rb', line 460 def week Utils.wrap_expr(_rbexpr.week) end |
#weekday ⇒ Expr
Extract the week day from the underlying Date representation.
Applies to Date and Datetime columns.
Returns the ISO weekday number where monday = 1 and sunday = 7
511 512 513 |
# File 'lib/polars/date_time_expr.rb', line 511 def weekday Utils.wrap_expr(_rbexpr.weekday) end |
#with_time_unit(tu) ⇒ Expr
Set time unit of a Series of dtype Datetime or Duration.
This does not modify underlying data, and should be used to fix an incorrect time unit.
960 961 962 |
# File 'lib/polars/date_time_expr.rb', line 960 def with_time_unit(tu) Utils.wrap_expr(_rbexpr.dt_with_time_unit(tu)) end |
#with_time_zone(tz) ⇒ Expr
Set time zone for a Series of type Datetime.
1039 1040 1041 |
# File 'lib/polars/date_time_expr.rb', line 1039 def with_time_zone(tz) Utils.wrap_expr(_rbexpr.dt_with_time_zone(tz)) end |
#year ⇒ Expr
Extract year from underlying Date representation.
Applies to Date and Datetime columns.
Returns the year number in the calendar date.
311 312 313 |
# File 'lib/polars/date_time_expr.rb', line 311 def year Utils.wrap_expr(_rbexpr.year) end |