Pandas - convert strings to time without date
After performing the conversion you can use the datetime accessor dt
to access just the hour
or time
component:
In [51]:
df['hour'] = pd.to_datetime(df['time'], format='%H:%M').dt.hour
df
Out[51]:
time hour
index
1 10:53 10
2 12:17 12
3 14:46 14
4 16:36 16
5 18:39 18
6 20:31 20
7 22:28 22
Also your format string H%:M%
is malformed, it's likely to raise a ValueError: ':' is a bad directive in format 'H%:M%'
Regarding your last comment the dtype is datetime.time
not datetime
:
In [53]:
df['time'].iloc[0]
Out[53]:
datetime.time(10, 53)
Python/Pandas convert string to time only
These two lines:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]
Can be written as:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'],format= '%H:%M:%S' ).dt.time
How to convert a column in pandas data frame as Time object
We can make use of to_timedelta
function in pandas
.
df['Time'] = pd.to_timedelta(df['Time'])
It will create time format of `timedelta64[ns]
Python pandas - Convert string to datetime without a year using pandas.to_datetime()
You can get the time part of a datetime series with Series.dt.time
print(data_log['LocalTime'].dt.time)
This series will consist of Python standard library datetime.time
objects.
Convert string to dateTime without the date information Python
I think this is because you are using datetime module so it is by default adding the starting date to it. you can extract just the time using
datetime.strptime("0:00:01.138000", "%H:%M:%S.%f").time()
Convert strings to pandas timestamps without date
If you only need to compare the them , you can do with
s=pd.Series(['14:12:2006' , '1200' , '1500']).str.replace(':','').astype(str)
s=s.str.ljust(s.str.len().max(),'0').astype(int)
s.iloc[1]<s.iloc[0]<s.iloc[2]
True
s
0 14122006
1 12000000
2 15000000
dtype: int64
Pandas Dataframe convert string to data without time
I would start by putting your dates in pd.datetime:
df['date'] = pd.to_datetime(df.date)
Now, you can see that the time component is still there:
df.date.values
array(['2014-06-28T19:00:00.000000000-0500'], dtype='datetime64[ns]')
If you are ok having a date object again, you want:
df['date'] = [x.strftime("%y-%m-%d") for x in df.date]
Here would be ending with a datetime:
df['date'] = [x.date() for x in df.date]
df.date
datetime.date(2014, 6, 29)
Converting Pandas Object to minutes and seconds
I guess what you're looking for is pd.to_timedelta (https://pandas.pydata.org/docs/reference/api/pandas.to_timedelta.html). to_datetime operation which will of course always try to create a date.
What you have to remember about though is that pd.to_timedelta could raise ValueError for your column, as it requires hh:mm:ss format. Try to use apply function on your column by adding '00:' by the beginning of arguments of your column (which I think are strings?), and then turn the column to timedelta. Could be something like:
pd.to_timedelta(perf_dfExtended['Stop_Time'].apply(lambda x: f'00:{x}'))
Related Topics
Stopping a Thread After a Certain Amount of Time
Type Object 'Datetime.Datetime' Has No Attribute 'Datetime'
How to Re Import an Updated Package While in Python Interpreter
Why Do Attribute References Act Like This with Python Inheritance
How Could I Use Batch Normalization in Tensorflow
Running an Interactive Command from Within Python
Multiple Ping Script in Python
How to Iterate Through Dictionary in a Dictionary in Django Template
Typeerror: List Indices Must Be Integers or Slices, Not Str
Pandas Groupby Without Turning Grouped by Column into Index
Python Variables as Keys to Dict
Importerror: No Module Named Win32Com.Client
Opencv Python Rotate Image by X Degrees Around Specific Point