separate datetime column in R while keeping time accurate
4/12/2016 12:00:00 AM I have dates in the format above and have tried to use separate() to create two columns in the data frame where the data is present. When I do the columns are created but AM/PM so the times just become numbers or worse appear as "12H 0M 0S". Can anyone help me out, pretty new to data analysis as a whole and would be much appreciated!
do you know?
how many words do you know
See also questions close to this topic
How to bring data frame into single column from multiple columns in python
I have data format in these multiple columns. So I want to bring all 4 columns of data into a single column.
YEAR Month pcp1 pcp2 pcp3 pcp4 1984 1 0 0 0 0 1984 2 1.2 0 0 0 1984 3 0 0 0 0 1984 4 0 0 0 0 1984 5 0 0 0 0 1984 6 0 0 0 1.6 1984 7 3 3 9.2 3.2 1984 8 6.2 27.1 5.4 0 1984 9 0 0 0 0 1984 10 0 0 0 0 1984 11 0 0 0 0 1984 12 0 0 0 0
How do I implement rank function for nearest values for a column in dataframe?
df.head(): run_time match_datetime country league home_team away_team 0 2021-08-07 00:04:36.326391 2021-08-06 Russia FNL 2 - Group 2 Yenisey 2 Lokomotiv-Kazanka 1 2021-08-07 00:04:36.326391 2021-08-07 Russia Youth League Ural U19 Krylya Sovetov Samara U19 2 2021-08-07 00:04:36.326391 2021-08-08 World Club Friendly Alaves Al Nasr 3 2021-08-07 00:04:36.326391 2021-08-09 China Jia League Chengdu Rongcheng Shenyang Urban FC 4 2021-08-06 00:04:36.326391 2021-08-06 China Super League Wuhan FC Tianjin Jinmen Tiger 5 2021-08-06 00:04:36.326391 2021-08-07 Czech Republic U19 League Sigma Olomouc U19 Karvina U19 6 2021-08-06 00:04:36.326391 2021-08-08 Russia Youth League Konoplev Academy U19 Rubin Kazan U19 7 2021-08-06 00:04:36.326391 2021-08-09 World Club Friendly Real Sociedad Eibar
run_time match_datetime country league home_team away_team 0 2021-08-07 00:04:36.326391 2021-08-06 Russia FNL 2 - Group 2 Yenisey 2 Lokomotiv-Kazanka 1 2021-08-07 00:04:36.326391 2021-08-07 Russia Youth League Ural U19 Krylya Sovetov Samara U19 4 2021-08-06 00:04:36.326391 2021-08-06 China Super League Wuhan FC Tianjin Jinmen Tiger 5 2021-08-06 00:04:36.326391 2021-08-07 Czech Republic U19 League Sigma Olomouc U19 Karvina U19
How do i use
rankfunction to filter only the 2 nearest
match_datetimedates for every
run_timevalue. i.e. desired dataframe will be a filtered dataframe that will have all the nearest 2
match_datetimevalues for every
A date loop problem and list remove problem on JupyterLab
It is very strange that the red circle of B should be displayed the same as the red circle of A. Why is week 6 missing?
And "if d.weekday() in [5, 6]: dates.remove(d)". It should be 5 and 6 removed, how can there be 4/3 and 4/10?
I have restarted the core and the result is the same. It's amazing...
Output the Tuesday 6 weeks in the future in Python?
UPDATE: post edited to add answer to end of post
Using Python, how do I output the date of the Tuesday that occurs 6 weeks after a certain date range?
I work at a SaaS company in a customer facing role. Whenever I do an implementation for a client, the client receives a survey email on the Tuesday that occurs in the 6th week after our initial interaction.
To know which Tuesday to be extra nice on, we currently have to reference a chart that says if the interaction falls in date range x, then the client receives their survey solicitation on Tuesday y.
An example of this would be: if the interaction happened sometime within Apr. 18 - Apr. 22, then the survey goes out on May 31.
I would prefer for this to be done without having to hard code the date ranges and their corresponding Tuesdays into my program (just because I'm lazy and don't want to update the dates manually as the months go by), but I'm open to that solution if that's how it has to be. :)
I can use datetime to output a particular date x weeks from today's date, but I'm not sure how to get from here to what I want to do.
import time from datetime import datetime, timedelta time1 = (time.strftime("%m/%d/%Y")) #current date time2 = ((datetime.now() + timedelta(weeks=6)).strftime('%m/%d/%Y')) #current date + six weeks print(time1) print((datetime.now() + timedelta(weeks=6)).strftime('%m/%d/%Y'))
Disclaimer: I am a beginner and although I did search for an answer to this question before posting, I may not have known the right terms to use. If this is a duplicate question, I would be thrilled to be pointed in the right direction. :)
Thanks to @Mandias for getting me on the right track. I was able to use week numbers to achieve my desired result.
from datetime import datetime, timedelta, date today = date.today() #get today's date todays_week = today.isocalendar() #get the current week number based on today's date survey_week = todays_week + 6 #add 6 weeks to the current week number todays_year = int(today.strftime("%Y")) #get today's calendar year and turn it from a str to an int survey_week_tuesday = date.fromisocalendar(todays_year, survey_week, 2) #(year, week, day of week) 2 is for Tuesday print("Current Week Number:") print(todays_week) print("Current Week Number + 6 Weeks:") print(todays_week + 6) print("Today's Year:") print(todays_year) print("The Tuesday on the 6th week from the current week (i.e. survey tuesday):") print(survey_week_tuesday.strftime('%m-%d-%Y')) #using strftime to format the survey date into MM-DD-YYYY format because that's what we use here even though DD-MM-YYYY makes more sense
Trying to create monthly counts but receiving error with lubridate
I am working with time series data and trying to count particular events across multiple years. I want to create a new.dataframe that shows the total count of events for each month/year, including 0 for months that have no events.
The following code worked for one set of data but is not working for two remaining datasets that are set up exactly the same.
Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) :
'from' must be a finite number
new.dataframe <- original.dataframe %>% group_by(var1, var2, .drop = FALSE) %>% drop_na(date_var) %>% mutate(month = lubridate::floor_date(date_var, unit = "months")) %>% count(month) %>% complete( month = seq.Date( min(month, na.rm=T), # include all months with no cases reported max(month, na.rm=T), by="month"), fill = list(n = 0)) new.dataframe <- new.dataframe %>% rename (monyear = month) new.dataframe$monyear <- format(new.dataframe$monyear, "%m/%Y")
The code runs fine if I remove the following line, however I want these variables:
group_by(var1, var2, .drop = FALSE) %>%
Does any one have any suggestions for that line of code?
Why does ggplot2 not accept lubridate time measurements?
I am currently trying to use Spotify's API data to show a frequency distribution of all of Taylor Swift's album's song lengths. I am using this code:
pacman::p_load(ggplot2, tidyverse) ts$duration <- seconds_to_period(ts$duration_ms/1000) # creating new column "duration" ggplot(ts, aes(x = duration, y = album_name, fill = album_name)) + # The graph geom_density_ridges2()
However, when I attempt to run it, I get the following errors:
Picking joint bandwidth of NaN Warning messages: 1: In min(data$x, na.rm = TRUE) : no non-missing arguments to min; returning Inf 2: In max(data$x, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 3: Removed 215 rows containing non-finite values (stat_density_ridges).
Why does ggplot2 not accept lubridate time measurements as an x axis?
Generate new variable based on start and stop date in dplyr
I am in need of assistance. I have some data that looks similar to this.
Machine Start Stop ServiceType 1 XX 2014-12-04 <NA> AA 2 XX 2013-09-05 2013-11-05 BB 3 XX 2013-11-21 2014-09-25 BB 4 XX 2013-10-11 2014-11-18 BB 5 XX 2021-12-03 <NA> AA 6 XX 2020-08-06 2022-09-15 AA 7 XX 2021-06-10 <NA> BB 8 YY 2020-01-17 <NA> BB 9 YY 2015-11-04 2018-04-30 BB 10 YY 2016-05-28 2019-03-21 BB 11 YY 2019-09-27 <NA> BB 12 YY 2018-01-05 <NA> AA
So what I would like to do is generate a new variable, say Maintenance or something, that's either
BBif only one service types is active or
BBare overlapping. Like,
Machine Date Maintenance 1 XX 2013-09-05 BB 2 XX 2013-11-21 BB 3 XX 2013-10-11 AA 4 XX 2014-12-04 CC 5 XX 2021-12-03 AA 6 YY 2015-11-04 BB 7 YY 2016-05-28 CC 8 YY 2020-01-17 BB
I have been working with
lubridatebut I am a bit unsure how to perform this task, any help would be kind.
NA's in this case can be considered a machine is on that service forever.