This question already has answers here:
Convert hh:mm:ss to minutes using python pandas
(3 answers)
Closed 5 years ago.
So I have Date/Time string in a pandas dataframe that looks like this:
2016-10-13 02:33:40
And I need to cut out the year/month/day completely, and convert the time to minutes. So that time/date above needs to be converted into just:
153
^^2 hours and 33 minutes = 153 minutes
I am basically trying to sift the data by the amount of time in between each entry and converting it all to minutes (since the amount of time that passes will not go past a day per session) seemed to make the most sense to me. But, I am open to any other suggestions!
Thanks for the help
lets say the date and time is in a column called DateTime.
df["Datetime"] = pd.to_datetime(df["Datetime"])
df["Minutes"] = 60*df["Datetime"].dt.hour +df["Datetime"].dt.minute
There once the datetime column is in the right format, you can access a lot of properties. See here for more https://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties
Related
This question already has answers here:
How to define format when using pandas to_datetime?
(2 answers)
Convert Pandas Column to DateTime
(8 answers)
Closed 5 months ago.
Does anyone know how to change 20130526T150000 to datetime format?
One note: the 'T' is usefull. Use pd.to_datetime() directly, the T is actually usefull as it denotes the iso format and will help not confuse for some locales (some countries have the month first, then the day, others the oposite - iso goes from most significant to less: year, month, day)...
pd.to_datetime("20130526T150000")
Timestamp('2013-05-26 15:00:00')
If you want to be more explicit, specify the format:
pd.to_datetime("20130526T150000", format=...)
However, this might be a duplicate: How to define format when using pandas to_datetime? ... For best results, if you are doing a conversion of a column, use Convert Pandas Column to DateTime
This question already has answers here:
Convert date from excel in number format to date format python [duplicate]
(2 answers)
Convert Excel style date with pandas
(3 answers)
Closed 1 year ago.
I'd like to convert a column to date. My source data is from an Excel which is already formatted to date data type. However, when pandas read my file the date columns are read as e.g.'44249'
I tried the following code
RPT["Pot"] =`RPT["Pot"].apply(lambda x: pd.to_datetime(x, format='%d%m%Y'))
but I got the error time data '44249' does not match format '%d%m%Y' (match).
I also tried this code:
RPT["PLANNED SUBMISSION DATE TO E&P"] = pd.to_datetime(RPT["PLANNED SUBMISSION DATE TO E&P"])
but the results were 1970-01-01 00:00:00.000044365, which inaccurate.
Can I anyone please help me?
This question already has answers here:
Calculate Time Difference Between Two Pandas Columns in Hours and Minutes
(4 answers)
How to calculate time difference between two pandas column [duplicate]
(2 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Hi I am trying to calculate time differences for certain tasks in some data I am working on. I have a csv file with a bunch of data, the relevant columns look like below:
ID
Start Date
End Date
123456
10/08/2021 02:00:05 AM
10/11/2021 01:00:15 AM
324524
10/11/2021 01:00:15 AM
10/08/2021 02:00:05 AM
My goal is to create a new file with the row ID, the start date, end date, and the time difference in hours.
So far I have used pandas.to_datetime to change the format of the start date and the end date. Now I am wondering how I can calculate the difference between the two times i.e. (end date - start date) in hours and create a new column in the dataframe to store it in.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I'm currently getting a panda data frame from alpaca - a stock trading platform. The data I'm getting is for minute data and is giving me a timestamp that cant be converted to DateTime to make my functions easier.
Heres an output for AMZN minute data
open high low
timestamp
2019-08-08 14:12:00-04:00 1826.8600 1826.9649 1826.1026
2019-08-08 14:13:00-04:00 1826.6500 1827.6520 1826.6500
Heres the data type:
print(type(AMZN.index[0]))
<class'pandas._libs.tslibs.timestamps.Timestamp'>
I'm trying to split this index into two columns where the day is the index and the time(minutes and hours) is the next column. Or just getting it into all into datetime format together would work fine too.
I've tried finding a similar problem and using _datetime() but it doesn't seem to work. I have the datetime library but it doesn't seem to register when I use the code as in other posts.
Overall Im just trying to find an easy way of iterating over a certain time on any given day and this format is giving me trouble. If someone has a better way of dealing with time series of this type please feel free to share.
Actually Timestamp is one of the easiest data type to manipulate in pandas:
df['Date'] = df.index.date
df['Time'] = df.index.time
df = df.set_index(df['Date])
Result:
open high low Time
Date
2019-08-08 1826.86 1826.9649 1826.1026 14:12:00
2019-08-08 1826.65 1827.6520 1826.6500 14:13:00
This question already has answers here:
Python TypeError: cannot convert the series to <class 'int'> when trying to do math on dataframe
(3 answers)
Closed 4 years ago.
I am working in a project now and the is a column known as 'Date of last recharge' but it's an object-type column but I need to convert it into date formats:
Date of Last Recharge
20-10-2018
23-10-2018
04-08-2018
12-09-2018
20-08-2018
How I went about it was to split each date into it's individual year(y), month(m) and day(d) by using custom functions. But in the process of trying to recombine each of the series into a new column using:
date(y,m,d)
But I end up with this error:
cannot convert the series to (class 'int')
I'm not even sure if this approach is correct, if you know a better way to do this, please let me know.
Try this, it will work ..
df['Date_of_Last_Recharge'] = pd.to_datetime(df['Date_of_Last_Recharge'])