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.
-
#combine(time, time_unit: "us") ⇒ Expr
Create a naive Datetime from an existing Date/Datetime expression and a Time.
-
#convert_time_zone(tz) ⇒ Expr
Set time zone for a Series of type Datetime.
-
#date ⇒ Expr
Date.
-
#datetime ⇒ Expr
Datetime.
-
#day ⇒ Expr
Extract day from underlying Date representation.
-
#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.
-
#is_leap_year ⇒ Expr
Determine whether the year of the underlying date is a leap year.
-
#iso_year ⇒ Expr
Extract ISO year from underlying Date representation.
-
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
-
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
-
#minute ⇒ Expr
Extract minutes from underlying DateTime representation.
-
#month ⇒ Expr
Extract month from underlying Date representation.
-
#month_end ⇒ Expr
Roll forward to the last day of the month.
-
#month_start ⇒ Expr
Roll backward to the first day of the month.
-
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
-
#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.
-
#replace_time_zone(time_zone, use_earliest: nil, ambiguous: "raise", non_existent: "raise") ⇒ Expr
Cast time zone for a Series of type Datetime.
-
#round(every, offset: nil) ⇒ Expr
Divide the date/datetime range into buckets.
-
#second(fractional: false) ⇒ Expr
Extract seconds from underlying DateTime representation.
-
#strftime(fmt) ⇒ Expr
Format Date/datetime with a formatting rule.
-
#time ⇒ Expr
Time.
-
#timestamp(tu = "us") ⇒ Expr
Return a timestamp in the given time unit.
-
#total_days ⇒ Expr
(also: #days)
Extract the days from a Duration type.
-
#total_hours ⇒ Expr
(also: #hours)
Extract the hours from a Duration type.
-
#total_microseconds ⇒ Expr
(also: #microseconds)
Extract the microseconds from a Duration type.
-
#total_milliseconds ⇒ Expr
(also: #milliseconds)
Extract the milliseconds from a Duration type.
-
#total_minutes ⇒ Expr
(also: #minutes)
Extract the minutes from a Duration type.
-
#total_nanoseconds ⇒ Expr
(also: #nanoseconds)
Extract the nanoseconds from a Duration type.
-
#total_seconds ⇒ Expr
(also: #seconds)
Extract the seconds from a Duration type.
-
#truncate(every, offset: nil, use_earliest: nil) ⇒ Expr
Divide the date/datetime range into buckets.
-
#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.
-
#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.
980 981 982 |
# File 'lib/polars/date_time_expr.rb', line 980 def cast_time_unit(tu) Utils.wrap_expr(_rbexpr.dt_cast_time_unit(tu)) end |
#combine(time, time_unit: "us") ⇒ Expr
Create a naive Datetime from an existing Date/Datetime expression and a Time.
If the underlying expression is a Datetime then its time component is replaced, and if it is a Date then a new Datetime is created by combining the two values.
237 238 239 240 241 242 243 |
# File 'lib/polars/date_time_expr.rb', line 237 def combine(time, time_unit: "us") unless time.is_a?(Time) || time.is_a?(Expr) raise TypeError, "expected 'time' to be a Ruby time or Polars expression, found #{time}" end time = Utils.expr_to_lit_or_expr(time) Utils.wrap_expr(_rbexpr.dt_combine(time._rbexpr, time_unit)) end |
#convert_time_zone(tz) ⇒ Expr
Set time zone for a Series of type Datetime.
1021 1022 1023 |
# File 'lib/polars/date_time_expr.rb', line 1021 def convert_time_zone(tz) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(tz)) end |
#date ⇒ Expr
Date
615 616 617 |
# File 'lib/polars/date_time_expr.rb', line 615 def date Utils.wrap_expr(_rbexpr.dt_date) end |
#datetime ⇒ Expr
Datetime
622 623 624 |
# File 'lib/polars/date_time_expr.rb', line 622 def datetime Utils.wrap_expr(_rbexpr.dt_datetime) 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.)
553 554 555 |
# File 'lib/polars/date_time_expr.rb', line 553 def day Utils.wrap_expr(_rbexpr.day) end |
#epoch(tu = "us") ⇒ Expr
Get the time passed since the Unix EPOCH in the give time unit.
863 864 865 866 867 868 869 870 871 872 873 |
# File 'lib/polars/date_time_expr.rb', line 863 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.
663 664 665 |
# File 'lib/polars/date_time_expr.rb', line 663 def hour Utils.wrap_expr(_rbexpr.hour) end |
#is_leap_year ⇒ Expr
Determine whether the year of the underlying date is a leap year.
Applies to Date and Datetime columns.
321 322 323 |
# File 'lib/polars/date_time_expr.rb', line 321 def is_leap_year Utils.wrap_expr(_rbexpr.dt_is_leap_year) 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.
333 334 335 |
# File 'lib/polars/date_time_expr.rb', line 333 def iso_year Utils.wrap_expr(_rbexpr.iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
821 822 823 |
# File 'lib/polars/date_time_expr.rb', line 821 def microsecond Utils.wrap_expr(_rbexpr.microsecond) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
812 813 814 |
# File 'lib/polars/date_time_expr.rb', line 812 def millisecond Utils.wrap_expr(_rbexpr.millisecond) end |
#minute ⇒ Expr
Extract minutes from underlying DateTime representation.
Applies to Datetime columns.
Returns the minute number from 0 to 59.
704 705 706 |
# File 'lib/polars/date_time_expr.rb', line 704 def minute Utils.wrap_expr(_rbexpr.minute) 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.
416 417 418 |
# File 'lib/polars/date_time_expr.rb', line 416 def month Utils.wrap_expr(_rbexpr.month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1440 1441 1442 |
# File 'lib/polars/date_time_expr.rb', line 1440 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1402 1403 1404 |
# File 'lib/polars/date_time_expr.rb', line 1402 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
830 831 832 |
# File 'lib/polars/date_time_expr.rb', line 830 def nanosecond Utils.wrap_expr(_rbexpr.nanosecond) 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.
1363 1364 1365 1366 |
# File 'lib/polars/date_time_expr.rb', line 1363 def offset_by(by) by = Utils.parse_as_expression(by, str_as_lit: true) 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.)
601 602 603 |
# File 'lib/polars/date_time_expr.rb', line 601 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.
374 375 376 |
# File 'lib/polars/date_time_expr.rb', line 374 def quarter Utils.wrap_expr(_rbexpr.quarter) end |
#replace_time_zone(time_zone, use_earliest: nil, ambiguous: "raise", non_existent: "raise") ⇒ Expr
Cast time zone for a Series of type Datetime.
Different from convert_time_zone
, this will also modify
the underlying timestamp,
1040 1041 1042 1043 1044 |
# File 'lib/polars/date_time_expr.rb', line 1040 def replace_time_zone(time_zone, use_earliest: nil, ambiguous: "raise", non_existent: "raise") ambiguous = Utils.rename_use_earliest_to_ambiguous(use_earliest, ambiguous) ambiguous = Polars.lit(ambiguous) unless ambiguous.is_a?(Expr) Utils.wrap_expr(_rbexpr.dt_replace_time_zone(time_zone, ambiguous._rbexpr, non_existent)) 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.
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/polars/date_time_expr.rb', line 213 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.
798 799 800 801 802 803 804 805 |
# File 'lib/polars/date_time_expr.rb', line 798 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 |
#strftime(fmt) ⇒ Expr
Format Date/datetime with a formatting rule.
250 251 252 |
# File 'lib/polars/date_time_expr.rb', line 250 def strftime(fmt) Utils.wrap_expr(_rbexpr.strftime(fmt)) end |
#time ⇒ Expr
Time
608 609 610 |
# File 'lib/polars/date_time_expr.rb', line 608 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(tu = "us") ⇒ Expr
Return a timestamp in the given time unit.
904 905 906 |
# File 'lib/polars/date_time_expr.rb', line 904 def (tu = "us") Utils.wrap_expr(_rbexpr.(tu)) end |
#total_days ⇒ Expr Also known as: days
Extract the days from a Duration type.
1075 1076 1077 |
# File 'lib/polars/date_time_expr.rb', line 1075 def total_days Utils.wrap_expr(_rbexpr.dt_total_days) end |
#total_hours ⇒ Expr Also known as: hours
Extract the hours from a Duration type.
1110 1111 1112 |
# File 'lib/polars/date_time_expr.rb', line 1110 def total_hours Utils.wrap_expr(_rbexpr.dt_total_hours) end |
#total_microseconds ⇒ Expr Also known as: microseconds
Extract the microseconds from a Duration type.
1265 1266 1267 |
# File 'lib/polars/date_time_expr.rb', line 1265 def total_microseconds Utils.wrap_expr(_rbexpr.dt_total_microseconds) end |
#total_milliseconds ⇒ Expr Also known as: milliseconds
Extract the milliseconds from a Duration type.
1223 1224 1225 |
# File 'lib/polars/date_time_expr.rb', line 1223 def total_milliseconds Utils.wrap_expr(_rbexpr.dt_total_milliseconds) end |
#total_minutes ⇒ Expr Also known as: minutes
Extract the minutes from a Duration type.
1145 1146 1147 |
# File 'lib/polars/date_time_expr.rb', line 1145 def total_minutes Utils.wrap_expr(_rbexpr.dt_total_minutes) end |
#total_nanoseconds ⇒ Expr Also known as: nanoseconds
Extract the nanoseconds from a Duration type.
1307 1308 1309 |
# File 'lib/polars/date_time_expr.rb', line 1307 def total_nanoseconds Utils.wrap_expr(_rbexpr.dt_total_nanoseconds) end |
#total_seconds ⇒ Expr Also known as: seconds
Extract the seconds from a Duration type.
1181 1182 1183 |
# File 'lib/polars/date_time_expr.rb', line 1181 def total_seconds Utils.wrap_expr(_rbexpr.dt_total_seconds) end |
#truncate(every, offset: nil, use_earliest: 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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/polars/date_time_expr.rb', line 100 def truncate(every, offset: nil, use_earliest: nil) if offset.nil? offset = "0ns" end if !every.is_a?(Expr) every = Utils._timedelta_to_pl_duration(every) end every = Utils.parse_as_expression(every, str_as_lit: true) Utils.wrap_expr( _rbexpr.dt_truncate( every, Utils._timedelta_to_pl_duration(offset), ) ) 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.)
458 459 460 |
# File 'lib/polars/date_time_expr.rb', line 458 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
505 506 507 |
# File 'lib/polars/date_time_expr.rb', line 505 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.
943 944 945 |
# File 'lib/polars/date_time_expr.rb', line 943 def with_time_unit(tu) Utils.wrap_expr(_rbexpr.dt_with_time_unit(tu)) end |
#year ⇒ Expr
Extract year from underlying Date representation.
Applies to Date and Datetime columns.
Returns the year number in the calendar date.
293 294 295 |
# File 'lib/polars/date_time_expr.rb', line 293 def year Utils.wrap_expr(_rbexpr.year) end |