This question already has answers here:
pandas - change time object to a float?
(3 answers)
Closed 2 years ago.
I need to convert my column with records like 'hh:mm:ss' to float format in order to make further calculations.
In excel it is done in very simple way, you just multiply 'hh:mm:ss' by 24, but in Python it doesn't work out. I'm new to Python, need your help.
Any idea?
My Dataframe:
list = ['01:36:01', '00:02:18', '02:59:40', '04:16:30']
Here is what I need to achieve:
lst = [['01:36:01', 1.600], ['00:02:18', 0.038] , ['02:59:40', 2.994], ['04:16:30', 4.275]]
df = pd.DataFrame(lst, columns = ['Time', 'Float'])
print(df)
Time Float
0 01:36:01 1.600
1 00:02:18 0.038
2 02:59:40 2.994
3 04:16:30 4.275
You can use below logic to find the time difference in seconds and then convert it into hours
import datetime as d
lis = ['01:36:01', '00:02:18', '02:59:40', '04:16:30']
start_dt = dt.datetime.strptime("00:00:00", '%H:%M:%S')
[float('{:0.3f}'.format((dt.datetime.strptime(time, '%H:%M:%S') - start_dt).seconds/3600)) for time in lis]
Output:
[1.6, 0.038, 2.994, 4.275]
Have you seen this post that looks like what you are trying to do.
https://stackoverflow.com/a/47043887/14982298
Related
This question already has answers here:
Formatting timedelta objects [duplicate]
(7 answers)
Closed 5 months ago.
I want to generate a timestamp only in hour and minutes format, like this:
154h 27m
Please check my logic below
planned_downtime = timedelta(hours=random.randrange(150))
However, the result is coming in seconds. To convert it to above mentioned format, I applied this:
planned_downtime.strftime("%H %M")
But I'm getting errors. How can I convert this randomly generated time in above format?
Like this maybe:
m = random.randint(0,60)
h = random.randint(0,24)
ts = f'{h}h {m}m'
print(ts)
15h 48m
This question already has answers here:
removing time from date&time variable in pandas?
(3 answers)
Closed last year.
solar["DATE"]= solar['DATE'].strftime('%Y-%m-%d')
display(solar)
I want to remove the time function from the DATE column. I only want the date, how do I get rid of it but keep the date?
[1]: https://i.stack.imgur.com/8G8Jg.png
The error I get is below:
AttributeError: 'Series' object has no attribute 'strftime'
According to the error i think so you are using pandas dataframe and to edit the values you will have to use .apply() function.
You can do it via:
#IF the values are already a datetime object
solar['DATE'].apply(lambda d: d.date())
#ELSE IF dates are a string:
solar['DATE'].apply(lambda d: d.stftime('%Y-%m-%d'))
What I came up with is what follows:
import pandas as pd
import datetime
date = pd.date_range("2018-01-01", periods=500, freq="H")
dataframe = pd.DataFrame({"date":date})
def removeDayTime(date):
dateStr = str(date) # This line is just to change the timestamp format to str. You probably do not need this line to include in your code.
dateWitoutTime = datetime.datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
return dateWitoutTime
dataframe["date"] = dataframe["date"].apply(removeDayTime)
dataframe.head()
Note that in order to have example data to work with, I have generated 500 periods of dates. You probably do not need to use my dataframe. So just use the rest of the code.
Output
date
0
2018-01-01
1
2018-01-01
2
2018-01-01
3
2018-01-01
4
2018-01-01
This question already has answers here:
Convert Excel style date with pandas
(3 answers)
Closed 1 year ago.
please I need solution to this problem I have a field that is formatted in e.g 43390 which is general date format in excel. I need to format it to a date like "d/m/yyy"
here is the code I wrote :
trans_ data['DATE'] = pd.to_ datetime(trans_ data['DATE'], format='%d-%m-%Y')
but I have this error:
ValueError: time data '43390' does not match format '%d-%m-%Y' (match)
I tried to convert the "43390" in LibreOffice and it converted it to 2018-10-17 (the origin "0" is "30/12/1899"):
origin = pd.Timestamp("30/12/1899")
df["col"] = df["col"].apply(lambda x: origin + pd.Timedelta(days=x))
print(df)
Prints:
col
0 2018-10-17
1 2019-11-02
df used:
col
0 43390
1 43771
Screenshot:
The following code might help you.
from datetime import timedelta
import pandas as pd
excel_date = '43390'
excel_date = int(excel_date)
python_date = pd.to_datetime('1900-01-01') + timedelta(excel_date-2)
print(python_date)
The python_date object stores the date. Then you can change the format to the format you need.
This question already has answers here:
Add months to a date in Pandas
(4 answers)
Closed 2 years ago.
I'm trying to work out the end date and have succeeded but code takes long to run. How can I improve the following code? Also df['end_date'] is a new variable? i tried:
df['end_date'] = [],
and appending it but getting a length error. I therefore wrote the below Many thanks, d
i = 0
j = 0
df['end_date'] = df['start_date']
for i in range(len(df['start_date'])):
for j in range(len(df['term'])):
df['end_date'] = (df['start_date'][i].date() + dt.timedelta(df['term'][j]*365/12))
i+=1
j+=1
my dataset looks like :
start_date term
2010-03-01 24
2009-11-01 36
2012-08-01 24
Assuming 'end_date' is a datetime, something like this should work:
df['end_date'] = df['start_date'] + pd.to_timedelta(df['term']*365/12, unit='d')
However, the proper way of handling month offsets would be to use pd.DateOffset:
# if the offset was the same for all records, it would be ok to just
# offsets = pd.DateOffset(months=the_offset)
# but here, we need to handle different values
offsets = df['term'].map(lambda term: pd.DateOffset(months=term))
df['end_date'] = df['start_date'] + offsets
If I understand correctly what you want to achieve, you should be able to do this:
df['end_date'] = df['start_date'].date() + dt.timedelta(df['term']*365/12)
You are looping over a dataframe, which you should avoid. Try to use the pandas API directly as much as possibly to use its underlying performance optimizations.
Also, you are manually incrementing i and j, but these are also the indices in your for loops and therefore incremented by the loop operator. So it should not be necessary to increment them manually.
This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 4 years ago.
I have a pandas dataframe with a column that should indicate the end of a financial quarter. The format is of the type "Q1-2009". Is there a quick way to convert these strings into a timestamp as "2009-03-31"?
I have found only the conversion from the format "YYYY-QQ", but not the opposite.
Create quarters periods with swap quarter and year part by replace and convert to datetimes with PeriodIndex.to_timestamp:
df = pd.DataFrame({'per':['Q1-2009','Q3-2007']})
df['date'] = (pd.PeriodIndex(df['per'].str.replace(r'(Q\d)-(\d+)', r'\2-\1'), freq='Q')
.to_timestamp(how='e'))
print (df)
per date
0 Q1-2009 2009-03-31
1 Q3-2007 2007-09-30
Another solution is use string indexing:
df['date'] = (pd.PeriodIndex(df['per'].str[-4:] + df['per'].str[:2], freq='Q')
.to_timestamp(how='e'))
One solution using a list comprehension followed by pd.offsets.MonthEnd:
# data from #jezrael
df = pd.DataFrame({'per':['Q1-2009','Q3-2007']})
def get_values(x):
''' Returns string with quarter number multiplied by 3 '''
return f'{int(x[0][1:])*3}-{x[1]}'
values = [get_values(x.split('-')) for x in df['per']]
df['LastDay'] = pd.to_datetime(values, format='%m-%Y') + pd.offsets.MonthEnd(1)
print(df)
per LastDay
0 Q1-2009 2009-03-31
1 Q3-2007 2007-09-30