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
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]) ⇒ 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], 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.
1865 1866 1867 |
# File 'lib/polars/date_time_expr.rb', line 1865 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.
1294 1295 1296 |
# File 'lib/polars/date_time_expr.rb', line 1294 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.
538 539 540 |
# File 'lib/polars/date_time_expr.rb', line 538 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.
1336 1337 1338 |
# File 'lib/polars/date_time_expr.rb', line 1336 def convert_time_zone(time_zone) Utils.wrap_expr(_rbexpr.dt_convert_time_zone(time_zone)) end |
#date ⇒ Expr
Date
932 933 934 |
# File 'lib/polars/date_time_expr.rb', line 932 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.)
857 858 859 |
# File 'lib/polars/date_time_expr.rb', line 857 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.
753 754 755 |
# File 'lib/polars/date_time_expr.rb', line 753 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).
1891 1892 1893 |
# File 'lib/polars/date_time_expr.rb', line 1891 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.
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
# File 'lib/polars/date_time_expr.rb', line 1217 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.
971 972 973 |
# File 'lib/polars/date_time_expr.rb', line 971 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.
599 600 601 602 603 604 605 606 607 608 |
# File 'lib/polars/date_time_expr.rb', line 599 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.
634 635 636 |
# File 'lib/polars/date_time_expr.rb', line 634 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.
667 668 669 |
# File 'lib/polars/date_time_expr.rb', line 667 def iso_year Utils.wrap_expr(_rbexpr.dt_iso_year) end |
#microsecond ⇒ Expr
Extract microseconds from underlying DateTime representation.
Applies to Datetime columns.
1148 1149 1150 |
# File 'lib/polars/date_time_expr.rb', line 1148 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.
500 501 502 |
# File 'lib/polars/date_time_expr.rb', line 500 def millennium Utils.wrap_expr(_rbexpr.dt_millennium) end |
#millisecond ⇒ Expr
Extract milliseconds from underlying DateTime representation.
Applies to Datetime columns.
1111 1112 1113 |
# File 'lib/polars/date_time_expr.rb', line 1111 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.
1010 1011 1012 |
# File 'lib/polars/date_time_expr.rb', line 1010 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.
724 725 726 |
# File 'lib/polars/date_time_expr.rb', line 724 def month Utils.wrap_expr(_rbexpr.dt_month) end |
#month_end ⇒ Expr
Roll forward to the last day of the month.
1835 1836 1837 |
# File 'lib/polars/date_time_expr.rb', line 1835 def month_end Utils.wrap_expr(_rbexpr.dt_month_end) end |
#month_start ⇒ Expr
Roll backward to the first day of the month.
1796 1797 1798 |
# File 'lib/polars/date_time_expr.rb', line 1796 def month_start Utils.wrap_expr(_rbexpr.dt_month_start) end |
#nanosecond ⇒ Expr
Extract nanoseconds from underlying DateTime representation.
Applies to Datetime columns.
1185 1186 1187 |
# File 'lib/polars/date_time_expr.rb', line 1185 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.
1756 1757 1758 1759 |
# File 'lib/polars/date_time_expr.rb', line 1756 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.)
895 896 897 |
# File 'lib/polars/date_time_expr.rb', line 895 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.
695 696 697 |
# File 'lib/polars/date_time_expr.rb', line 695 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,
1418 1419 1420 1421 1422 1423 |
# File 'lib/polars/date_time_expr.rb', line 1418 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.
1069 1070 1071 1072 1073 1074 1075 1076 |
# File 'lib/polars/date_time_expr.rb', line 1069 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.
462 463 464 |
# File 'lib/polars/date_time_expr.rb', line 462 def strftime(format) Utils.wrap_expr(_rbexpr.strftime(format)) end |
#time ⇒ Expr
Time
902 903 904 |
# File 'lib/polars/date_time_expr.rb', line 902 def time Utils.wrap_expr(_rbexpr.dt_time) end |
#timestamp(time_unit = "us") ⇒ Expr
Return a timestamp in the given time unit.
1257 1258 1259 |
# File 'lib/polars/date_time_expr.rb', line 1257 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.
401 402 403 404 405 406 |
# File 'lib/polars/date_time_expr.rb', line 401 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.
1457 1458 1459 |
# File 'lib/polars/date_time_expr.rb', line 1457 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.
1494 1495 1496 |
# File 'lib/polars/date_time_expr.rb', line 1494 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.
1657 1658 1659 |
# File 'lib/polars/date_time_expr.rb', line 1657 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.
1613 1614 1615 |
# File 'lib/polars/date_time_expr.rb', line 1613 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.
1531 1532 1533 |
# File 'lib/polars/date_time_expr.rb', line 1531 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.
1701 1702 1703 |
# File 'lib/polars/date_time_expr.rb', line 1701 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.
1569 1570 1571 |
# File 'lib/polars/date_time_expr.rb', line 1569 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.
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.)
782 783 784 |
# File 'lib/polars/date_time_expr.rb', line 782 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
819 820 821 |
# File 'lib/polars/date_time_expr.rb', line 819 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.
569 570 571 |
# File 'lib/polars/date_time_expr.rb', line 569 def year Utils.wrap_expr(_rbexpr.dt_year) end |