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
-
#add_business_days(n, week_mask: [true, true, true, true, true, false, false], roll: "raise") ⇒ Expr
Offset by
n
business days. -
#base_utc_offset ⇒ Expr
Base offset from UTC.
-
#cast_time_unit(time_unit) ⇒ Expr
Cast the underlying data to another time unit.
-
#century ⇒ Expr
Extract the century from underlying representation.
-
#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_business_day(week_mask: [true, true, true, true, true, false, false]) ⇒ Expr
Determine whether each day lands on a business day.
-
#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.
-
#millennium ⇒ Expr
Extract the millennium from underlying 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(year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil, microsecond: nil, ambiguous: "raise") ⇒ Expr
Replace time unit.
-
#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
#add_business_days(n, week_mask: [true, true, true, true, true, false, false], roll: "raise") ⇒ Expr
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
Offset by n
business days.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/polars/date_time_expr.rb', line 47 def add_business_days( n, week_mask: [true, true, true, true, true, false, false], roll: "raise" ) n_rbexpr = Utils.parse_into_expression(n) Utils.wrap_expr( _rbexpr.dt_add_business_days( n_rbexpr, week_mask, [], roll ) ) end |
#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.
1862 1863 1864 |
# File 'lib/polars/date_time_expr.rb', line 1862 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.
1305 1306 1307 |
# File 'lib/polars/date_time_expr.rb', line 1305 def cast_time_unit(time_unit) Utils.wrap_expr(_rbexpr.dt_cast_time_unit(time_unit)) end |
#century ⇒ Expr
Extract the century from underlying representation.
Applies to Date and Datetime columns.
Returns the century number in the calendar date.
530 531 532 |
# File 'lib/polars/date_time_expr.rb', line 530 def century Utils.wrap_expr(_rbexpr.dt_century) 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.
355 356 357 358 359 360 361 |
# File 'lib/polars/date_time_expr.rb', line 355 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.
1347 1348 1349 |
# File 'lib/polars/date_time_expr.rb', line 1347 def convert_time_zone(time_zone) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone)) end |
#date ⇒ Expr
Date
895 896 897 |
# File 'lib/polars/date_time_expr.rb', line 895 def date Utils.wrap_expr(_rbexpr.dt_date) end |
#datetime ⇒ Expr
Datetime
902 903 904 |
# File 'lib/polars/date_time_expr.rb', line 902 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.)
820 821 822 |
# File 'lib/polars/date_time_expr.rb', line 820 def day Utils.wrap_expr(_rbexpr.dt_day) end |
#dst_offset ⇒ Expr
Additional offset currently in effect (typically due to daylight saving time).
1888 1889 1890 |
# File 'lib/polars/date_time_expr.rb', line 1888 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.
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 |
# File 'lib/polars/date_time_expr.rb', line 1187 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.
941 942 943 |
# File 'lib/polars/date_time_expr.rb', line 941 def hour Utils.wrap_expr(_rbexpr.dt_hour) end |
#is_business_day(week_mask: [true, true, true, true, true, false, false]) ⇒ Expr
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
Determine whether each day lands on a business day.
591 592 593 594 595 596 597 598 599 600 |
# File 'lib/polars/date_time_expr.rb', line 591 def is_business_day( week_mask: [true, true, true, true, true, false, false] ) Utils.wrap_expr( _rbexpr.dt_is_business_day( week_mask, [] ) ) end |
#is_leap_year ⇒ Expr
Determine whether the year of the underlying date is a leap year.
Applies to Date and Datetime columns.
626 627 628 |
# File 'lib/polars/date_time_expr.rb', line 626 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.
659 660 661 |
# File 'lib/polars/date_time_expr.rb', line 659 def iso_year Utils.wrap_expr(_rbexpr.dt_iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
1118 1119 1120 |
# File 'lib/polars/date_time_expr.rb', line 1118 def microsecond Utils.wrap_expr(_rbexpr.dt_microsecond) end |
#millennium ⇒ Expr
Extract the millennium from underlying representation.
Applies to Date and Datetime columns.
Returns the millennium number in the calendar date.
492 493 494 |
# File 'lib/polars/date_time_expr.rb', line 492 def millennium Utils.wrap_expr(_rbexpr.dt_millennium) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
1081 1082 1083 |
# File 'lib/polars/date_time_expr.rb', line 1081 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.
980 981 982 |
# File 'lib/polars/date_time_expr.rb', line 980 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.
716 717 718 |
# File 'lib/polars/date_time_expr.rb', line 716 def month Utils.wrap_expr(_rbexpr.dt_month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1832 1833 1834 |
# File 'lib/polars/date_time_expr.rb', line 1832 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1793 1794 1795 |
# File 'lib/polars/date_time_expr.rb', line 1793 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
1155 1156 1157 |
# File 'lib/polars/date_time_expr.rb', line 1155 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.
1753 1754 1755 1756 |
# File 'lib/polars/date_time_expr.rb', line 1753 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.)
858 859 860 |
# File 'lib/polars/date_time_expr.rb', line 858 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.
687 688 689 |
# File 'lib/polars/date_time_expr.rb', line 687 def quarter Utils.wrap_expr(_rbexpr.dt_quarter) end |
#replace(year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil, microsecond: nil, ambiguous: "raise") ⇒ Expr
Replace time unit.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/polars/date_time_expr.rb', line 314 def replace( year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil, microsecond: nil, ambiguous: "raise" ) day, month, year, hour, minute, second, microsecond = ( Utils.parse_into_list_of_expressions( day, month, year, hour, minute, second, microsecond ) ) ambiguous_expr = Utils.parse_into_expression(ambiguous, str_as_lit: true) Utils.wrap_expr( _rbexpr.dt_replace( year, month, day, hour, minute, second, microsecond, ambiguous_expr ) ) 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,
1429 1430 1431 1432 1433 1434 |
# File 'lib/polars/date_time_expr.rb', line 1429 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.
252 253 254 255 |
# File 'lib/polars/date_time_expr.rb', line 252 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.
1039 1040 1041 1042 1043 1044 1045 1046 |
# File 'lib/polars/date_time_expr.rb', line 1039 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.
454 455 456 |
# File 'lib/polars/date_time_expr.rb', line 454 def strftime(fmt) Utils.wrap_expr(_rbexpr.strftime(fmt)) end |
#time ⇒ Expr
Time
865 866 867 |
# File 'lib/polars/date_time_expr.rb', line 865 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
1227 1228 1229 |
# File 'lib/polars/date_time_expr.rb', line 1227 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.
401 402 403 |
# File 'lib/polars/date_time_expr.rb', line 401 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.
1465 1466 1467 |
# File 'lib/polars/date_time_expr.rb', line 1465 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.
1500 1501 1502 |
# File 'lib/polars/date_time_expr.rb', line 1500 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.
1655 1656 1657 |
# File 'lib/polars/date_time_expr.rb', line 1655 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.
1613 1614 1615 |
# File 'lib/polars/date_time_expr.rb', line 1613 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.
1535 1536 1537 |
# File 'lib/polars/date_time_expr.rb', line 1535 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.
1697 1698 1699 |
# File 'lib/polars/date_time_expr.rb', line 1697 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.
1571 1572 1573 |
# File 'lib/polars/date_time_expr.rb', line 1571 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.
158 159 160 161 162 163 164 165 |
# File 'lib/polars/date_time_expr.rb', line 158 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.)
745 746 747 |
# File 'lib/polars/date_time_expr.rb', line 745 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
782 783 784 |
# File 'lib/polars/date_time_expr.rb', line 782 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.
1268 1269 1270 |
# File 'lib/polars/date_time_expr.rb', line 1268 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.
561 562 563 |
# File 'lib/polars/date_time_expr.rb', line 561 def year Utils.wrap_expr(_rbexpr.dt_year) end |