Skip to content

Supported Functions

using TidierData
using TidierDates

mdy(), dmy(), ymd()¤

These functions parse dates represented as strings into a DateTime format in Julia. The input should be a string month-day-year, day-month-year, or year-month-day format respectively. They are relatively robust in their ability to take non-uniform strings of dates with year in yyyy format

df = DataFrame(date = ["today is the 4th July, 2000",
                        "ayer fue 13th Oct 2001",
                        "3 of Mar, 2002 was a fun day",
                        "23rd Apr 2003",
                        "23/7/2043",
                        "03/2/1932",
                        "23-08-1932",
                        "4th of July, 2005",
                        "08092019" ,
                        missing])

@chain df begin
    @mutate(date = dmy(date))
end
10×1 DataFrame
Rowdate
Date?
12000-07-04
22001-10-13
32002-03-03
42003-04-23
52043-07-23
61932-02-03
71932-08-23
82005-07-04
92019-09-08
10missing

mdy_hms(), dmy_hms(), ymd_hms()¤

Similar to the previous group, these functions parse date-time strings in month-day-year, day-month-year, or year-month-day format respectively. The input should include both date and time information.

round_date(), floor_date()¤

#`floor_date()`: This function rounds a date down to the nearest specified unit (e.g., hour, minute, day, month, year). It takes two arguments - a Date or DateTime object and a string indicating the unit of time to which the date should be floored.
#`round_date()`: This function rounds a date to the nearest specified unit (e.g., hour, minute, month, year). Like


df2 = DataFrame(date = ["20190330120141", "2008-04-05 16-23-07", "2010.06.07 19:45:00",
                        "2011-2-8 14-3-7", "2012-3, 9 09:2, 37", "201305-15 0302-09",
                        "2013 arbitrary 2 non-decimal 7 chars 13 in between 2 !!! 7",
                        "OR collapsed formats: 20140618 181608 (as long as prefixed with zeros)",
                         missing ])

@chain df2 begin
    @mutate(date = ymd_hms(date))
    @mutate(floor_byhr = floor_date(date, "hour"))
    @mutate(round_bymin = round_date(date, "minute"))
    @mutate(rounded_bymo = round_date(date, "month"))
end
9×4 DataFrame
Rowdatefloor_byhrround_byminrounded_bymo
DateTime?DateTime?DateTime?DateTime?
12019-03-30T12:01:412019-03-30T12:00:002019-03-30T12:02:002019-04-01T00:00:00
22008-04-05T16:23:072008-04-05T16:00:002008-04-05T16:23:002008-04-01T00:00:00
32010-06-07T19:45:002010-06-07T19:00:002010-06-07T19:45:002010-06-01T00:00:00
42011-02-08T14:03:072011-02-08T14:00:002011-02-08T14:03:002011-02-01T00:00:00
52012-03-09T09:02:372012-03-09T09:00:002012-03-09T09:03:002012-03-01T00:00:00
62013-05-15T03:02:092013-05-15T03:00:002013-05-15T03:02:002013-05-01T00:00:00
72013-02-12T07:13:022013-02-12T07:00:002013-02-12T07:13:002013-02-01T00:00:00
82014-06-18T18:16:082014-06-18T18:00:002014-06-18T18:16:002014-07-01T00:00:00
9missingmissingmissingmissing

difftime()¤

This function computes the difference between two DateTime or Date objects. It returns the result in the unit specified by the second argument, which can be "seconds", "minutes", "hours", "days", or "weeks". It returns this value as a float.

times = DataFrame(
    start_time = [
        "06-27-2023 15:20:00",
        "06-26-2023 12:45:15",
        "06-26-2023 16:30:30",
        "06-25-2023 10:11:35",
        "06-24-2023 09:00:24",
        "06-26-2023 09:30:00",
        "06-25-2023 11:00:15",
        "06-24-2023 01:34:45",
        "06-26-2023 14:20:00",
        "06-25-2023 10:45:30"
    ],
    end_time = [
        "06-27-2023 14:53:53",
        "06-25-2023 10:50:30",
        "06-28-2023 16:32:30",
        "06-24-2023 10:20:30",
        "06-24-2023 10:05:00",
         missing,
        "10-25-2023 11:55:13",
        "06-24-2023 11:35:45",
        "07-26-2023 15:15:45",
        "06-24-2023 12:50:15"
    ]
)
10×2 DataFrame
Rowstart_timeend_time
StringString?
106-27-2023 15:20:0006-27-2023 14:53:53
206-26-2023 12:45:1506-25-2023 10:50:30
306-26-2023 16:30:3006-28-2023 16:32:30
406-25-2023 10:11:3506-24-2023 10:20:30
506-24-2023 09:00:2406-24-2023 10:05:00
606-26-2023 09:30:00missing
706-25-2023 11:00:1510-25-2023 11:55:13
806-24-2023 01:34:4506-24-2023 11:35:45
906-26-2023 14:20:0007-26-2023 15:15:45
1006-25-2023 10:45:3006-24-2023 12:50:15

After a string is converted into a datetime format, Date.jl functions such as hour(), year(), etc can be applied in Tidier chains as well.

@chain times begin
    @mutate(start_time = mdy_hms(start_time))
    @mutate(end_time = mdy_hms(end_time))
    @mutate(timedifmins = difftime(end_time, start_time, "minutes"))
    @mutate(timedifmins = difftime(end_time, start_time, "hours"))
    @mutate(year= year(start_time))
    @mutate(second = second(start_time))
end
10×5 DataFrame
Rowstart_timeend_timetimedifminsyearsecond
DateTimeDateTime?Float64?Int64Int64
12023-06-27T15:20:002023-06-27T14:53:53-0.43527820230
22023-06-26T12:45:152023-06-25T10:50:30-25.9125202315
32023-06-26T16:30:302023-06-28T16:32:3048.0333202330
42023-06-25T10:11:352023-06-24T10:20:30-23.8514202335
52023-06-24T09:00:242023-06-24T10:05:001.07667202324
62023-06-26T09:30:00missingmissing20230
72023-06-25T11:00:152023-10-25T11:55:132928.92202315
82023-06-24T01:34:452023-06-24T11:35:4510.0167202345
92023-06-26T14:20:002023-07-26T15:15:45720.92920230
102023-06-25T10:45:302023-06-24T12:50:15-21.9208202330

hms()¤

This function parses time strings (e.g., "12:34:56") into a Time format in Julia. It takes a string or an array of strings with the time information and doesn't require additional arguments.

df3 = DataFrame(
    Time = [
        "09:00:24",
        "10:11:35",
        "01:34:45",
        "12:45:15",
        "09:30:00",
        "10:45:30",
        "11:00:15",
        "14:20:00",
        "15:10:45",
        missing
    ]
)

@chain df3 begin
    @mutate(Time = hms(Time))
    @filter(!ismissing(Time))
    @mutate(hour = hour(Time))
end
9×2 DataFrame
RowTimehour
Time?Int64
109:00:249
210:11:3510
301:34:451
412:45:1512
509:30:009
610:45:3010
711:00:1511
814:20:0014
915:10:4515

This page was generated using Literate.jl.