Splitting timestamp column into seperate date and time columns
up vote
13
down vote
favorite
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
python pandas
add a comment |
up vote
13
down vote
favorite
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
python pandas
1
How about'2016-02-22 14:59:44.561776'.split()?
– pp_
Feb 24 '16 at 7:25
1
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27
add a comment |
up vote
13
down vote
favorite
up vote
13
down vote
favorite
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
python pandas
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
python pandas
python pandas
edited Feb 24 '16 at 7:24
wrkyle
357521
357521
asked Feb 24 '16 at 7:21
Tom
95118
95118
1
How about'2016-02-22 14:59:44.561776'.split()?
– pp_
Feb 24 '16 at 7:25
1
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27
add a comment |
1
How about'2016-02-22 14:59:44.561776'.split()?
– pp_
Feb 24 '16 at 7:25
1
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27
1
1
How about
'2016-02-22 14:59:44.561776'.split()?– pp_
Feb 24 '16 at 7:25
How about
'2016-02-22 14:59:44.561776'.split()?– pp_
Feb 24 '16 at 7:25
1
1
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27
add a comment |
7 Answers
7
active
oldest
votes
up vote
12
down vote
accepted
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)
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Trypd.to_datetime(df.my_timestamp)to see if that converts it correctly.
– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result ofdf.my_timestamp.iat[0]? This will return the contents of your first timestamp.dfis obviously the name of your dataframe, andmy_timestampis the name of your date column.
– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907
– Tom
Feb 24 '16 at 8:39
|
show 2 more comments
up vote
9
down vote
I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:
df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time
In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
add a comment |
up vote
4
down vote
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.
add a comment |
up vote
2
down vote
If your timestamps are already in pandas format (not string), then:
df["date"] = df["timestamp"].date
dt["time"] = dt["timestamp"].time
If your timestamp is a string, you can parse it using the datetime module:
from datetime import datetime
data1["timestamp"] = df["timestamp"].apply(lambda x:
datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))
Source:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
add a comment |
up vote
2
down vote
If your timestamp is a string, you can convert it to a datetime object:
from datetime import datetime
timestamp = '2016-02-22 14:59:44.561776'
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
From then on you can bring it to whatever format you like.
add a comment |
up vote
0
down vote
Try
s = '2016-02-22 14:59:44.561776'
date,time = s.split()
then convert time as needed.
If you want to further split the time,
hour, minute, second = time.split(':')
add a comment |
up vote
0
down vote
try this:
def time_date(datetime_obj):
date_time = datetime_obj.split(' ')
time = date_time[1].split('.')
return date_time[0], time[0]
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
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)
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Trypd.to_datetime(df.my_timestamp)to see if that converts it correctly.
– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result ofdf.my_timestamp.iat[0]? This will return the contents of your first timestamp.dfis obviously the name of your dataframe, andmy_timestampis the name of your date column.
– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907
– Tom
Feb 24 '16 at 8:39
|
show 2 more comments
up vote
12
down vote
accepted
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)
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Trypd.to_datetime(df.my_timestamp)to see if that converts it correctly.
– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result ofdf.my_timestamp.iat[0]? This will return the contents of your first timestamp.dfis obviously the name of your dataframe, andmy_timestampis the name of your date column.
– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907
– Tom
Feb 24 '16 at 8:39
|
show 2 more comments
up vote
12
down vote
accepted
up vote
12
down vote
accepted
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)
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)
edited Feb 2 at 23:44
answered Feb 24 '16 at 7:33
Alexander
51.5k1184119
51.5k1184119
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Trypd.to_datetime(df.my_timestamp)to see if that converts it correctly.
– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result ofdf.my_timestamp.iat[0]? This will return the contents of your first timestamp.dfis obviously the name of your dataframe, andmy_timestampis the name of your date column.
– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907
– Tom
Feb 24 '16 at 8:39
|
show 2 more comments
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Trypd.to_datetime(df.my_timestamp)to see if that converts it correctly.
– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result ofdf.my_timestamp.iat[0]? This will return the contents of your first timestamp.dfis obviously the name of your dataframe, andmy_timestampis the name of your date column.
– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907
– Tom
Feb 24 '16 at 8:39
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.
– Tom
Feb 24 '16 at 8:18
That means your data is formatted as strings instead of timestamps. Try
pd.to_datetime(df.my_timestamp) to see if that converts it correctly.– Alexander
Feb 24 '16 at 8:21
That means your data is formatted as strings instead of timestamps. Try
pd.to_datetime(df.my_timestamp) to see if that converts it correctly.– Alexander
Feb 24 '16 at 8:21
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?
– Tom
Feb 24 '16 at 8:31
To see exactly what the string looks like, can you please post the result of
df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.– Alexander
Feb 24 '16 at 8:36
To see exactly what the string looks like, can you please post the result of
df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.– Alexander
Feb 24 '16 at 8:36
2016-02-21 21:19:44.797907– Tom
Feb 24 '16 at 8:39
2016-02-21 21:19:44.797907– Tom
Feb 24 '16 at 8:39
|
show 2 more comments
up vote
9
down vote
I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:
df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time
In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
add a comment |
up vote
9
down vote
I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:
df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time
In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
add a comment |
up vote
9
down vote
up vote
9
down vote
I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:
df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time
In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000
I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:
df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time
In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000
edited Feb 24 '16 at 8:22
answered Feb 24 '16 at 7:47
Anton Protopopov
14.3k34657
14.3k34657
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
add a comment |
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
3
3
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.
– Sean_Calgary
Mar 15 at 16:34
This should be the accepted answer.
– user32185
Nov 14 at 19:00
This should be the accepted answer.
– user32185
Nov 14 at 19:00
add a comment |
up vote
4
down vote
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.
add a comment |
up vote
4
down vote
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.
add a comment |
up vote
4
down vote
up vote
4
down vote
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.
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.
answered Nov 20 '17 at 9:20
Nodar Okroshiashvili
542314
542314
add a comment |
add a comment |
up vote
2
down vote
If your timestamps are already in pandas format (not string), then:
df["date"] = df["timestamp"].date
dt["time"] = dt["timestamp"].time
If your timestamp is a string, you can parse it using the datetime module:
from datetime import datetime
data1["timestamp"] = df["timestamp"].apply(lambda x:
datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))
Source:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
add a comment |
up vote
2
down vote
If your timestamps are already in pandas format (not string), then:
df["date"] = df["timestamp"].date
dt["time"] = dt["timestamp"].time
If your timestamp is a string, you can parse it using the datetime module:
from datetime import datetime
data1["timestamp"] = df["timestamp"].apply(lambda x:
datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))
Source:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
add a comment |
up vote
2
down vote
up vote
2
down vote
If your timestamps are already in pandas format (not string), then:
df["date"] = df["timestamp"].date
dt["time"] = dt["timestamp"].time
If your timestamp is a string, you can parse it using the datetime module:
from datetime import datetime
data1["timestamp"] = df["timestamp"].apply(lambda x:
datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))
Source:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
If your timestamps are already in pandas format (not string), then:
df["date"] = df["timestamp"].date
dt["time"] = dt["timestamp"].time
If your timestamp is a string, you can parse it using the datetime module:
from datetime import datetime
data1["timestamp"] = df["timestamp"].apply(lambda x:
datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))
Source:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
answered Feb 24 '16 at 7:35
Mathieu B
305211
305211
add a comment |
add a comment |
up vote
2
down vote
If your timestamp is a string, you can convert it to a datetime object:
from datetime import datetime
timestamp = '2016-02-22 14:59:44.561776'
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
From then on you can bring it to whatever format you like.
add a comment |
up vote
2
down vote
If your timestamp is a string, you can convert it to a datetime object:
from datetime import datetime
timestamp = '2016-02-22 14:59:44.561776'
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
From then on you can bring it to whatever format you like.
add a comment |
up vote
2
down vote
up vote
2
down vote
If your timestamp is a string, you can convert it to a datetime object:
from datetime import datetime
timestamp = '2016-02-22 14:59:44.561776'
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
From then on you can bring it to whatever format you like.
If your timestamp is a string, you can convert it to a datetime object:
from datetime import datetime
timestamp = '2016-02-22 14:59:44.561776'
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
From then on you can bring it to whatever format you like.
edited Feb 24 '16 at 18:24
answered Feb 24 '16 at 7:39
pp_
2,76131226
2,76131226
add a comment |
add a comment |
up vote
0
down vote
Try
s = '2016-02-22 14:59:44.561776'
date,time = s.split()
then convert time as needed.
If you want to further split the time,
hour, minute, second = time.split(':')
add a comment |
up vote
0
down vote
Try
s = '2016-02-22 14:59:44.561776'
date,time = s.split()
then convert time as needed.
If you want to further split the time,
hour, minute, second = time.split(':')
add a comment |
up vote
0
down vote
up vote
0
down vote
Try
s = '2016-02-22 14:59:44.561776'
date,time = s.split()
then convert time as needed.
If you want to further split the time,
hour, minute, second = time.split(':')
Try
s = '2016-02-22 14:59:44.561776'
date,time = s.split()
then convert time as needed.
If you want to further split the time,
hour, minute, second = time.split(':')
answered Feb 24 '16 at 7:27
wrkyle
357521
357521
add a comment |
add a comment |
up vote
0
down vote
try this:
def time_date(datetime_obj):
date_time = datetime_obj.split(' ')
time = date_time[1].split('.')
return date_time[0], time[0]
add a comment |
up vote
0
down vote
try this:
def time_date(datetime_obj):
date_time = datetime_obj.split(' ')
time = date_time[1].split('.')
return date_time[0], time[0]
add a comment |
up vote
0
down vote
up vote
0
down vote
try this:
def time_date(datetime_obj):
date_time = datetime_obj.split(' ')
time = date_time[1].split('.')
return date_time[0], time[0]
try this:
def time_date(datetime_obj):
date_time = datetime_obj.split(' ')
time = date_time[1].split('.')
return date_time[0], time[0]
edited Feb 24 '16 at 9:59
Fabio Lamanna
7,74464786
7,74464786
answered Feb 24 '16 at 7:33
Nitin Sanghi
341
341
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35595710%2fsplitting-timestamp-column-into-seperate-date-and-time-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How about
'2016-02-22 14:59:44.561776'.split()?– pp_
Feb 24 '16 at 7:25
1
Why on earth would you want to do that?!?
– Alexander
Feb 24 '16 at 7:27