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
-
#base_utc_offset ⇒ Expr
Base offset from UTC.
-
#cast_time_unit(time_unit) ⇒ 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(time_zone) ⇒ Expr
Set time zone for a Series of type Datetime.
-
#date ⇒ Expr
Date.
-
#datetime ⇒ Expr
Datetime.
-
#day ⇒ Expr
Extract day from underlying Date representation.
-
#dst_offset ⇒ Expr
Additional offset currently in effect (typically due to daylight saving time).
-
#epoch(time_unit = "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, ambiguous: "raise", non_existent: "raise") ⇒ Expr
Cast time zone for a Series of type Datetime.
-
#round(every) ⇒ 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(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
-
#to_string(format) ⇒ Expr
Convert a Date/Time/Datetime column into a String column with the given format.
-
#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) ⇒ 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(time_unit) ⇒ Expr
Set time unit of a Series of dtype Datetime or Duration.
-
#year ⇒ Expr
Extract year from underlying Date representation.
Instance Method Details
#base_utc_offset ⇒ Expr
Base offset from UTC.
This is usually constant for all datetimes in a given time zone, but may vary in the rare case that a country switches time zone, like Samoa (Apia) did at the end of 2011.
1611 1612 1613 |
# File 'lib/polars/date_time_expr.rb', line 1611 def base_utc_offset Utils.wrap_expr(_rbexpr.dt_base_utc_offset) end |
#cast_time_unit(time_unit) ⇒ Expr
Cast the underlying data to another time unit. This may lose precision.
1054 1055 1056 |
# File 'lib/polars/date_time_expr.rb', line 1054 def cast_time_unit(time_unit) Utils.wrap_expr(_rbexpr.dt_cast_time_unit(time_unit)) 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.
217 218 219 220 221 222 223 |
# File 'lib/polars/date_time_expr.rb', line 217 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.parse_into_expression(time) Utils.wrap_expr(_rbexpr.dt_combine(time, time_unit)) end |
#convert_time_zone(time_zone) ⇒ Expr
Set time zone for a Series of type Datetime.
1096 1097 1098 |
# File 'lib/polars/date_time_expr.rb', line 1096 def convert_time_zone(time_zone) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone)) end |
#date ⇒ Expr
Date
644 645 646 |
# File 'lib/polars/date_time_expr.rb', line 644 def date Utils.wrap_expr(_rbexpr.dt_date) end |
#datetime ⇒ Expr
Datetime
651 652 653 |
# File 'lib/polars/date_time_expr.rb', line 651 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.)
569 570 571 |
# File 'lib/polars/date_time_expr.rb', line 569 def day Utils.wrap_expr(_rbexpr.dt_day) end |
#dst_offset ⇒ Expr
Additional offset currently in effect (typically due to daylight saving time).
1637 1638 1639 |
# File 'lib/polars/date_time_expr.rb', line 1637 def dst_offset Utils.wrap_expr(_rbexpr.dt_dst_offset) end |
#epoch(time_unit = "us") ⇒ Expr
Get the time passed since the Unix EPOCH in the give time unit.
936 937 938 939 940 941 942 943 944 945 946 |
# File 'lib/polars/date_time_expr.rb', line 936 def epoch(time_unit = "us") if Utils::DTYPE_TEMPORAL_UNITS.include?(time_unit) (time_unit) elsif time_unit == "s" Utils.wrap_expr(_rbexpr.dt_epoch_seconds) elsif time_unit == "d" Utils.wrap_expr(_rbexpr).cast(:date).cast(:i32) else raise ArgumentError, "time_unit must be one of {'ns', 'us', 'ms', 's', 'd'}, got #{time_unit.inspect}" end end |
#hour ⇒ Expr
Extract hour from underlying DateTime representation.
Applies to Datetime columns.
Returns the hour number from 0 to 23.
690 691 692 |
# File 'lib/polars/date_time_expr.rb', line 690 def hour Utils.wrap_expr(_rbexpr.dt_hour) end |
#is_leap_year ⇒ Expr
Determine whether the year of the underlying date is a leap year.
Applies to Date and Datetime columns.
375 376 377 |
# File 'lib/polars/date_time_expr.rb', line 375 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.
408 409 410 |
# File 'lib/polars/date_time_expr.rb', line 408 def iso_year Utils.wrap_expr(_rbexpr.dt_iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
867 868 869 |
# File 'lib/polars/date_time_expr.rb', line 867 def microsecond Utils.wrap_expr(_rbexpr.dt_microsecond) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
830 831 832 |
# File 'lib/polars/date_time_expr.rb', line 830 def millisecond Utils.wrap_expr(_rbexpr.dt_millisecond) end |
#minute ⇒ Expr
Extract minutes from underlying DateTime representation.
Applies to Datetime columns.
Returns the minute number from 0 to 59.
729 730 731 |
# File 'lib/polars/date_time_expr.rb', line 729 def minute Utils.wrap_expr(_rbexpr.dt_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.
465 466 467 |
# File 'lib/polars/date_time_expr.rb', line 465 def month Utils.wrap_expr(_rbexpr.dt_month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1581 1582 1583 |
# File 'lib/polars/date_time_expr.rb', line 1581 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1542 1543 1544 |
# File 'lib/polars/date_time_expr.rb', line 1542 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
904 905 906 |
# File 'lib/polars/date_time_expr.rb', line 904 def nanosecond Utils.wrap_expr(_rbexpr.dt_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.
1502 1503 1504 1505 |
# File 'lib/polars/date_time_expr.rb', line 1502 def offset_by(by) by = Utils.parse_into_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.)
607 608 609 |
# File 'lib/polars/date_time_expr.rb', line 607 def ordinal_day Utils.wrap_expr(_rbexpr.dt_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.
436 437 438 |
# File 'lib/polars/date_time_expr.rb', line 436 def quarter Utils.wrap_expr(_rbexpr.dt_quarter) end |
#replace_time_zone(time_zone, 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,
1178 1179 1180 1181 1182 1183 |
# File 'lib/polars/date_time_expr.rb', line 1178 def replace_time_zone(time_zone, ambiguous: "raise", non_existent: "raise") unless ambiguous.is_a?(Expr) ambiguous = Polars.lit(ambiguous) end Utils.wrap_expr(_rbexpr.dt_replace_time_zone(time_zone, ambiguous._rbexpr, non_existent)) end |
#round(every) ⇒ 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.
201 202 203 204 |
# File 'lib/polars/date_time_expr.rb', line 201 def round(every) every = Utils.parse_into_expression(every, str_as_lit: true) Utils.wrap_expr(_rbexpr.dt_round(every)) 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.
788 789 790 791 792 793 794 795 |
# File 'lib/polars/date_time_expr.rb', line 788 def second(fractional: false) sec = Utils.wrap_expr(_rbexpr.dt_second) if fractional sec + (Utils.wrap_expr(_rbexpr.dt_nanosecond) / F.lit(1_000_000_000.0)) else sec end end |
#strftime(fmt) ⇒ Expr
Format Date/datetime with a formatting rule.
316 317 318 |
# File 'lib/polars/date_time_expr.rb', line 316 def strftime(fmt) Utils.wrap_expr(_rbexpr.strftime(fmt)) end |
#time ⇒ Expr
Time
614 615 616 |
# File 'lib/polars/date_time_expr.rb', line 614 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
976 977 978 |
# File 'lib/polars/date_time_expr.rb', line 976 def (time_unit = "us") Utils.wrap_expr(_rbexpr.(time_unit)) end |
#to_string(format) ⇒ Expr
Convert a Date/Time/Datetime column into a String column with the given format.
Similar to cast(Polars::String)
, but this method allows you to customize the
formatting of the resulting string.
263 264 265 |
# File 'lib/polars/date_time_expr.rb', line 263 def to_string(format) Utils.wrap_expr(_rbexpr.dt_to_string(format)) end |
#total_days ⇒ Expr Also known as: days
Extract the days from a Duration type.
1214 1215 1216 |
# File 'lib/polars/date_time_expr.rb', line 1214 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.
1249 1250 1251 |
# File 'lib/polars/date_time_expr.rb', line 1249 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.
1404 1405 1406 |
# File 'lib/polars/date_time_expr.rb', line 1404 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.
1362 1363 1364 |
# File 'lib/polars/date_time_expr.rb', line 1362 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.
1284 1285 1286 |
# File 'lib/polars/date_time_expr.rb', line 1284 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.
1446 1447 1448 |
# File 'lib/polars/date_time_expr.rb', line 1446 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.
1320 1321 1322 |
# File 'lib/polars/date_time_expr.rb', line 1320 def total_seconds Utils.wrap_expr(_rbexpr.dt_total_seconds) end |
#truncate(every) ⇒ Expr
The every
argument is 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.
107 108 109 110 111 112 113 114 |
# File 'lib/polars/date_time_expr.rb', line 107 def truncate(every) if !every.is_a?(Expr) every = Utils.parse_as_duration_string(every) end every = Utils.parse_into_expression(every, str_as_lit: true) Utils.wrap_expr(_rbexpr.dt_truncate(every)) 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.)
494 495 496 |
# File 'lib/polars/date_time_expr.rb', line 494 def week Utils.wrap_expr(_rbexpr.dt_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
531 532 533 |
# File 'lib/polars/date_time_expr.rb', line 531 def weekday Utils.wrap_expr(_rbexpr.dt_weekday) end |
#with_time_unit(time_unit) ⇒ 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.
1017 1018 1019 |
# File 'lib/polars/date_time_expr.rb', line 1017 def with_time_unit(time_unit) Utils.wrap_expr(_rbexpr.dt_with_time_unit(time_unit)) end |
#year ⇒ Expr
Extract year from underlying Date representation.
Applies to Date and Datetime columns.
Returns the year number in the calendar date.
347 348 349 |
# File 'lib/polars/date_time_expr.rb', line 347 def year Utils.wrap_expr(_rbexpr.dt_year) end |