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.
1367 1368 1369 |
# File 'lib/polars/date_time_expr.rb', line 1367 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.
875 876 877 |
# File 'lib/polars/date_time_expr.rb', line 875 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.
917 918 919 |
# File 'lib/polars/date_time_expr.rb', line 917 def convert_time_zone(time_zone) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone)) end |
#date ⇒ Expr
Date
577 578 579 |
# File 'lib/polars/date_time_expr.rb', line 577 def date Utils.wrap_expr(_rbexpr.dt_date) end |
#datetime ⇒ Expr
Datetime
584 585 586 |
# File 'lib/polars/date_time_expr.rb', line 584 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.)
525 526 527 |
# File 'lib/polars/date_time_expr.rb', line 525 def day Utils.wrap_expr(_rbexpr.dt_day) end |
#dst_offset ⇒ Expr
Additional offset currently in effect (typically due to daylight saving time).
1393 1394 1395 |
# File 'lib/polars/date_time_expr.rb', line 1393 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.
785 786 787 788 789 790 791 792 793 794 795 |
# File 'lib/polars/date_time_expr.rb', line 785 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.
623 624 625 |
# File 'lib/polars/date_time_expr.rb', line 623 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.
331 332 333 |
# File 'lib/polars/date_time_expr.rb', line 331 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.
364 365 366 |
# File 'lib/polars/date_time_expr.rb', line 364 def iso_year Utils.wrap_expr(_rbexpr.dt_iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
744 745 746 |
# File 'lib/polars/date_time_expr.rb', line 744 def microsecond Utils.wrap_expr(_rbexpr.dt_microsecond) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
735 736 737 |
# File 'lib/polars/date_time_expr.rb', line 735 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.
662 663 664 |
# File 'lib/polars/date_time_expr.rb', line 662 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.
421 422 423 |
# File 'lib/polars/date_time_expr.rb', line 421 def month Utils.wrap_expr(_rbexpr.dt_month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1337 1338 1339 |
# File 'lib/polars/date_time_expr.rb', line 1337 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1298 1299 1300 |
# File 'lib/polars/date_time_expr.rb', line 1298 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
753 754 755 |
# File 'lib/polars/date_time_expr.rb', line 753 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.
1258 1259 1260 1261 |
# File 'lib/polars/date_time_expr.rb', line 1258 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.)
563 564 565 |
# File 'lib/polars/date_time_expr.rb', line 563 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.
392 393 394 |
# File 'lib/polars/date_time_expr.rb', line 392 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,
934 935 936 937 938 939 |
# File 'lib/polars/date_time_expr.rb', line 934 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.
721 722 723 724 725 726 727 728 |
# File 'lib/polars/date_time_expr.rb', line 721 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.
272 273 274 |
# File 'lib/polars/date_time_expr.rb', line 272 def strftime(fmt) Utils.wrap_expr(_rbexpr.strftime(fmt)) end |
#time ⇒ Expr
Time
570 571 572 |
# File 'lib/polars/date_time_expr.rb', line 570 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
825 826 827 |
# File 'lib/polars/date_time_expr.rb', line 825 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.
970 971 972 |
# File 'lib/polars/date_time_expr.rb', line 970 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.
1005 1006 1007 |
# File 'lib/polars/date_time_expr.rb', line 1005 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.
1160 1161 1162 |
# File 'lib/polars/date_time_expr.rb', line 1160 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.
1118 1119 1120 |
# File 'lib/polars/date_time_expr.rb', line 1118 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.
1040 1041 1042 |
# File 'lib/polars/date_time_expr.rb', line 1040 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.
1202 1203 1204 |
# File 'lib/polars/date_time_expr.rb', line 1202 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.
1076 1077 1078 |
# File 'lib/polars/date_time_expr.rb', line 1076 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.)
450 451 452 |
# File 'lib/polars/date_time_expr.rb', line 450 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
487 488 489 |
# File 'lib/polars/date_time_expr.rb', line 487 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.
838 839 840 |
# File 'lib/polars/date_time_expr.rb', line 838 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.
303 304 305 |
# File 'lib/polars/date_time_expr.rb', line 303 def year Utils.wrap_expr(_rbexpr.dt_year) end |