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], holidays: [], roll: "raise") ⇒ Expr
Offset by
nbusiness 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.
-
#day ⇒ Expr
Extract day from underlying Date representation.
-
#days_in_month ⇒ Expr
Extract the number of days in the month from the 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], holidays: []) ⇒ 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(format) ⇒ Expr
Convert a Date/Time/Datetime column into a String column with the given format.
-
#time ⇒ Expr
Time.
-
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
-
#to_string(format = nil) ⇒ Expr
Convert a Date/Time/Datetime column into a String column with the given format.
-
#total_days(fractional: false) ⇒ Expr
Extract the days from a Duration type.
-
#total_hours(fractional: false) ⇒ Expr
Extract the hours from a Duration type.
-
#total_microseconds(fractional: false) ⇒ Expr
Extract the microseconds from a Duration type.
-
#total_milliseconds(fractional: false) ⇒ Expr
Extract the milliseconds from a Duration type.
-
#total_minutes(fractional: false) ⇒ Expr
Extract the minutes from a Duration type.
-
#total_nanoseconds(fractional: false) ⇒ Expr
Extract the nanoseconds from a Duration type.
-
#total_seconds(fractional: false) ⇒ Expr
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.
-
#year ⇒ Expr
Extract year from underlying Date representation.
Instance Method Details
#add_business_days(n, week_mask: [true, true, true, true, true, false, false], holidays: [], 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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/polars/date_time_expr.rb', line 97 def add_business_days( n, week_mask: [true, true, true, true, true, false, false], holidays: [], roll: "raise" ) n_rbexpr = Utils.parse_into_expression(n) holidays_rbexpr = Utils._holidays_to_expr(holidays) Utils.wrap_expr( _rbexpr.dt_add_business_days( n_rbexpr, week_mask, holidays_rbexpr, 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.
1953 1954 1955 |
# File 'lib/polars/date_time_expr.rb', line 1953 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.
1382 1383 1384 |
# File 'lib/polars/date_time_expr.rb', line 1382 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.
590 591 592 |
# File 'lib/polars/date_time_expr.rb', line 590 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.
407 408 409 410 411 412 413 |
# File 'lib/polars/date_time_expr.rb', line 407 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.
1424 1425 1426 |
# File 'lib/polars/date_time_expr.rb', line 1424 def convert_time_zone(time_zone) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone)) end |
#date ⇒ Expr
Date
1020 1021 1022 |
# File 'lib/polars/date_time_expr.rb', line 1020 def date Utils.wrap_expr(_rbexpr.dt_date) 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.)
945 946 947 |
# File 'lib/polars/date_time_expr.rb', line 945 def day Utils.wrap_expr(_rbexpr.dt_day) end |
#days_in_month ⇒ Expr
Extract the number of days in the month from the underlying Date representation.
Applies to Date and Datetime columns.
Returns the number of days in the month. The return value ranges from 28 to 31.
841 842 843 |
# File 'lib/polars/date_time_expr.rb', line 841 def days_in_month Utils.wrap_expr(_rbexpr.dt_days_in_month) end |
#dst_offset ⇒ Expr
Additional offset currently in effect (typically due to daylight saving time).
1979 1980 1981 |
# File 'lib/polars/date_time_expr.rb', line 1979 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.
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
# File 'lib/polars/date_time_expr.rb', line 1305 def epoch(time_unit = "us") if Utils::DTYPE_TEMPORAL_UNITS.include?(time_unit) (time_unit) elsif time_unit == "s" ("ms").floordiv(F.lit(1000, dtype: Int64)) elsif time_unit == "d" Utils.wrap_expr(_rbexpr).cast(Date).cast(Int32) 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.
1059 1060 1061 |
# File 'lib/polars/date_time_expr.rb', line 1059 def hour Utils.wrap_expr(_rbexpr.dt_hour) end |
#is_business_day(week_mask: [true, true, true, true, true, false, false], holidays: []) ⇒ 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.
685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/polars/date_time_expr.rb', line 685 def is_business_day( week_mask: [true, true, true, true, true, false, false], holidays: [] ) holidays_rbexpr = Utils._holidays_to_expr(holidays) Utils.wrap_expr( _rbexpr.dt_is_business_day( week_mask, holidays_rbexpr ) ) end |
#is_leap_year ⇒ Expr
Determine whether the year of the underlying date is a leap year.
Applies to Date and Datetime columns.
722 723 724 |
# File 'lib/polars/date_time_expr.rb', line 722 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.
755 756 757 |
# File 'lib/polars/date_time_expr.rb', line 755 def iso_year Utils.wrap_expr(_rbexpr.dt_iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
1236 1237 1238 |
# File 'lib/polars/date_time_expr.rb', line 1236 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.
552 553 554 |
# File 'lib/polars/date_time_expr.rb', line 552 def millennium Utils.wrap_expr(_rbexpr.dt_millennium) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
1199 1200 1201 |
# File 'lib/polars/date_time_expr.rb', line 1199 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.
1098 1099 1100 |
# File 'lib/polars/date_time_expr.rb', line 1098 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.
812 813 814 |
# File 'lib/polars/date_time_expr.rb', line 812 def month Utils.wrap_expr(_rbexpr.dt_month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1923 1924 1925 |
# File 'lib/polars/date_time_expr.rb', line 1923 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1884 1885 1886 |
# File 'lib/polars/date_time_expr.rb', line 1884 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
1273 1274 1275 |
# File 'lib/polars/date_time_expr.rb', line 1273 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.
1844 1845 1846 1847 |
# File 'lib/polars/date_time_expr.rb', line 1844 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.)
983 984 985 |
# File 'lib/polars/date_time_expr.rb', line 983 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.
783 784 785 |
# File 'lib/polars/date_time_expr.rb', line 783 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.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/polars/date_time_expr.rb', line 366 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,
1506 1507 1508 1509 1510 1511 |
# File 'lib/polars/date_time_expr.rb', line 1506 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.
304 305 306 307 |
# File 'lib/polars/date_time_expr.rb', line 304 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.
1157 1158 1159 1160 1161 1162 1163 1164 |
# File 'lib/polars/date_time_expr.rb', line 1157 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(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.
514 515 516 |
# File 'lib/polars/date_time_expr.rb', line 514 def strftime(format) Utils.wrap_expr(_rbexpr.strftime(format)) end |
#time ⇒ Expr
Time
990 991 992 |
# File 'lib/polars/date_time_expr.rb', line 990 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
1345 1346 1347 |
# File 'lib/polars/date_time_expr.rb', line 1345 def (time_unit = "us") Utils.wrap_expr(_rbexpr.(time_unit)) end |
#to_string(format = nil) ⇒ 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.
453 454 455 456 457 458 |
# File 'lib/polars/date_time_expr.rb', line 453 def to_string(format = nil) if format.nil? format = "iso" end Utils.wrap_expr(_rbexpr.dt_to_string(format)) end |
#total_days(fractional: false) ⇒ Expr
Extract the days from a Duration type.
1545 1546 1547 |
# File 'lib/polars/date_time_expr.rb', line 1545 def total_days(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_days(fractional)) end |
#total_hours(fractional: false) ⇒ Expr
Extract the hours from a Duration type.
1582 1583 1584 |
# File 'lib/polars/date_time_expr.rb', line 1582 def total_hours(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_hours(fractional)) end |
#total_microseconds(fractional: false) ⇒ Expr
Extract the microseconds from a Duration type.
1745 1746 1747 |
# File 'lib/polars/date_time_expr.rb', line 1745 def total_microseconds(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_microseconds(fractional)) end |
#total_milliseconds(fractional: false) ⇒ Expr
Extract the milliseconds from a Duration type.
1701 1702 1703 |
# File 'lib/polars/date_time_expr.rb', line 1701 def total_milliseconds(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_milliseconds(fractional)) end |
#total_minutes(fractional: false) ⇒ Expr
Extract the minutes from a Duration type.
1619 1620 1621 |
# File 'lib/polars/date_time_expr.rb', line 1619 def total_minutes(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_minutes(fractional)) end |
#total_nanoseconds(fractional: false) ⇒ Expr
Extract the nanoseconds from a Duration type.
1789 1790 1791 |
# File 'lib/polars/date_time_expr.rb', line 1789 def total_nanoseconds(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_nanoseconds(fractional)) end |
#total_seconds(fractional: false) ⇒ Expr
Extract the seconds from a Duration type.
1657 1658 1659 |
# File 'lib/polars/date_time_expr.rb', line 1657 def total_seconds(fractional: false) Utils.wrap_expr(_rbexpr.dt_total_seconds(fractional)) 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.
210 211 212 213 214 215 216 217 |
# File 'lib/polars/date_time_expr.rb', line 210 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.)
870 871 872 |
# File 'lib/polars/date_time_expr.rb', line 870 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
907 908 909 |
# File 'lib/polars/date_time_expr.rb', line 907 def weekday Utils.wrap_expr(_rbexpr.dt_weekday) end |
#year ⇒ Expr
Extract year from underlying Date representation.
Applies to Date and Datetime columns.
Returns the year number in the calendar date.
621 622 623 |
# File 'lib/polars/date_time_expr.rb', line 621 def year Utils.wrap_expr(_rbexpr.dt_year) end |