How to set missing Year, Month and Day for Panda's Datetime
up vote
1
down vote
favorite
I have timestamp data in the format %H:%M:%S:%f
. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?
Code I'm using:
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)
Output:
Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009
Desired output:
A set date, so for instance:
Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009
python pandas datetime
add a comment |
up vote
1
down vote
favorite
I have timestamp data in the format %H:%M:%S:%f
. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?
Code I'm using:
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)
Output:
Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009
Desired output:
A set date, so for instance:
Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009
python pandas datetime
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have timestamp data in the format %H:%M:%S:%f
. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?
Code I'm using:
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)
Output:
Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009
Desired output:
A set date, so for instance:
Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009
python pandas datetime
I have timestamp data in the format %H:%M:%S:%f
. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f
How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?
Code I'm using:
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)
Output:
Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009
Desired output:
A set date, so for instance:
Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009
python pandas datetime
python pandas datetime
edited Nov 21 at 20:16
Ken Dekalb
14411
14411
asked Nov 21 at 20:11
Chrisvdberge
5211822
5211822
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
add a comment |
up vote
0
down vote
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
2
why not do all that in one application ofreplace
; e.g:dt.replace(2018, 11, 21)
?
– Sam Mason
Nov 21 at 23:02
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
add a comment |
up vote
3
down vote
accepted
There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards
import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
answered Nov 21 at 20:18
Sven Harris
1,6871211
1,6871211
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
add a comment |
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
doh! seems simple enough, thx!
– Chrisvdberge
Nov 21 at 20:27
add a comment |
up vote
0
down vote
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
2
why not do all that in one application ofreplace
; e.g:dt.replace(2018, 11, 21)
?
– Sam Mason
Nov 21 at 23:02
add a comment |
up vote
0
down vote
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
2
why not do all that in one application ofreplace
; e.g:dt.replace(2018, 11, 21)
?
– Sam Mason
Nov 21 at 23:02
add a comment |
up vote
0
down vote
up vote
0
down vote
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
edited Nov 22 at 11:39
answered Nov 21 at 20:17
Tom Ron
1,099619
1,099619
2
why not do all that in one application ofreplace
; e.g:dt.replace(2018, 11, 21)
?
– Sam Mason
Nov 21 at 23:02
add a comment |
2
why not do all that in one application ofreplace
; e.g:dt.replace(2018, 11, 21)
?
– Sam Mason
Nov 21 at 23:02
2
2
why not do all that in one application of
replace
; e.g: dt.replace(2018, 11, 21)
?– Sam Mason
Nov 21 at 23:02
why not do all that in one application of
replace
; e.g: dt.replace(2018, 11, 21)
?– Sam Mason
Nov 21 at 23:02
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%2f53419798%2fhow-to-set-missing-year-month-and-day-for-pandas-datetime%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