I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:
2016-02-22 14:59:44.561776
I'm having a hard time splitting this time stamp into 2 columns- 'date' and 'time'. The date format can stay the same, but the time needs to be converted to CST (including milliseconds).
Thanks for the help
Method 1: Use SPLIT Function Since the timestamp is composed of Date and Time, we can use the SPLIT function to extract the Date to one cell and Time to another cell. Here are the steps: On cell B2, type =SPLIT(A2, “ ”). This will automatically write the date in cell B2 and the time in cell C2.
Had same problem and this worked for me.
Suppose the date column in your dataset is called "date"
import pandas as pd df = pd.read_csv(file_path) df['Dates'] = pd.to_datetime(df['date']).dt.date df['Time'] = pd.to_datetime(df['date']).dt.time
This will give you two columns "Dates" and "Time" with splited dates.
I'm not sure why you would want to do this in the first place, but if you really must...
df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)}) >>> df my_timestamp 0 2016-01-01 15:00:00 1 2016-01-02 15:00:00 2 2016-01-03 15:00:00 3 2016-01-04 15:00:00 4 2016-01-05 15:00:00 df['new_date'] = [d.date() for d in df['my_timestamp']] df['new_time'] = [d.time() for d in df['my_timestamp']] >>> df my_timestamp new_date new_time 0 2016-01-01 15:00:00 2016-01-01 15:00:00 1 2016-01-02 15:00:00 2016-01-02 15:00:00 2 2016-01-03 15:00:00 2016-01-03 15:00:00 3 2016-01-04 15:00:00 2016-01-04 15:00:00 4 2016-01-05 15:00:00 2016-01-05 15:00:00
The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?
For more details:
https://docs.python.org/2/library/datetime.html
How to make an unaware datetime timezone aware in python
EDIT
An alternative method that only loops once across the timestamps instead of twice:
new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']]) df = df.assign(new_date=new_dates, new_time=new_times)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With