Convert dataframe to quarter
up vote
1
down vote
favorite
I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:
df:
Date
0 201601
1 201602
2 201603
3 201604
201601
is the first quarter of 2016 and 201604
is the fourth quarter of 2016. The desired outcome is:
df:
Date
0 2016-03-31
1 2016-06-30
2 2016-09-30
3 2016-12-31
This is what I tried, but it doesn't work.
df['date'] = pd.to_datetime(df.Date, format = '%Y%q')
Thanks!
python
add a comment |
up vote
1
down vote
favorite
I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:
df:
Date
0 201601
1 201602
2 201603
3 201604
201601
is the first quarter of 2016 and 201604
is the fourth quarter of 2016. The desired outcome is:
df:
Date
0 2016-03-31
1 2016-06-30
2 2016-09-30
3 2016-12-31
This is what I tried, but it doesn't work.
df['date'] = pd.to_datetime(df.Date, format = '%Y%q')
Thanks!
python
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:
df:
Date
0 201601
1 201602
2 201603
3 201604
201601
is the first quarter of 2016 and 201604
is the fourth quarter of 2016. The desired outcome is:
df:
Date
0 2016-03-31
1 2016-06-30
2 2016-09-30
3 2016-12-31
This is what I tried, but it doesn't work.
df['date'] = pd.to_datetime(df.Date, format = '%Y%q')
Thanks!
python
I have a dataframe (df). The Date variable is a string. I want to convert it to date and reformat it to the date at the end of the quarter. A sample is below:
df:
Date
0 201601
1 201602
2 201603
3 201604
201601
is the first quarter of 2016 and 201604
is the fourth quarter of 2016. The desired outcome is:
df:
Date
0 2016-03-31
1 2016-06-30
2 2016-09-30
3 2016-12-31
This is what I tried, but it doesn't work.
df['date'] = pd.to_datetime(df.Date, format = '%Y%q')
Thanks!
python
python
asked Nov 19 at 14:20
Roger
5911
5911
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You could define a function to compute the date and then apply that function.
Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:
from calendar import monthrange
from datetime import datetime
def end_quarter(quarter):
year = int(quarter[:4])
month = int(quarter[-2:]) * 3
day = monthrange(year, month)[1]
return datetime(year, month, day).date()
and you could then apply it using:
df["Date"] = df["Date"].apply(end_quarter)
add a comment |
up vote
0
down vote
One way is to keep treating it as a string, and simply do an if / else statement, i.e.
res =
for i in df['Date']:
v1 = i[-2:]
if v1 == '01':
res.append(i[:4] + '-03-31')
elif v1 == '02':
res.append(i[:4] + '-06-30')
elif v1 == '03':
res.append(i[4:] + '-09-30')
else:
res.append(i[4:] + '-12-31')
#>>> res
#['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You could define a function to compute the date and then apply that function.
Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:
from calendar import monthrange
from datetime import datetime
def end_quarter(quarter):
year = int(quarter[:4])
month = int(quarter[-2:]) * 3
day = monthrange(year, month)[1]
return datetime(year, month, day).date()
and you could then apply it using:
df["Date"] = df["Date"].apply(end_quarter)
add a comment |
up vote
1
down vote
You could define a function to compute the date and then apply that function.
Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:
from calendar import monthrange
from datetime import datetime
def end_quarter(quarter):
year = int(quarter[:4])
month = int(quarter[-2:]) * 3
day = monthrange(year, month)[1]
return datetime(year, month, day).date()
and you could then apply it using:
df["Date"] = df["Date"].apply(end_quarter)
add a comment |
up vote
1
down vote
up vote
1
down vote
You could define a function to compute the date and then apply that function.
Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:
from calendar import monthrange
from datetime import datetime
def end_quarter(quarter):
year = int(quarter[:4])
month = int(quarter[-2:]) * 3
day = monthrange(year, month)[1]
return datetime(year, month, day).date()
and you could then apply it using:
df["Date"] = df["Date"].apply(end_quarter)
You could define a function to compute the date and then apply that function.
Using the monthrange function from the calendar module (https://docs.python.org/3.7/library/calendar.html#calendar.monthrange), your function might look something like:
from calendar import monthrange
from datetime import datetime
def end_quarter(quarter):
year = int(quarter[:4])
month = int(quarter[-2:]) * 3
day = monthrange(year, month)[1]
return datetime(year, month, day).date()
and you could then apply it using:
df["Date"] = df["Date"].apply(end_quarter)
edited Nov 20 at 10:26
answered Nov 19 at 14:48
Owen
3,1301914
3,1301914
add a comment |
add a comment |
up vote
0
down vote
One way is to keep treating it as a string, and simply do an if / else statement, i.e.
res =
for i in df['Date']:
v1 = i[-2:]
if v1 == '01':
res.append(i[:4] + '-03-31')
elif v1 == '02':
res.append(i[:4] + '-06-30')
elif v1 == '03':
res.append(i[4:] + '-09-30')
else:
res.append(i[4:] + '-12-31')
#>>> res
#['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']
add a comment |
up vote
0
down vote
One way is to keep treating it as a string, and simply do an if / else statement, i.e.
res =
for i in df['Date']:
v1 = i[-2:]
if v1 == '01':
res.append(i[:4] + '-03-31')
elif v1 == '02':
res.append(i[:4] + '-06-30')
elif v1 == '03':
res.append(i[4:] + '-09-30')
else:
res.append(i[4:] + '-12-31')
#>>> res
#['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']
add a comment |
up vote
0
down vote
up vote
0
down vote
One way is to keep treating it as a string, and simply do an if / else statement, i.e.
res =
for i in df['Date']:
v1 = i[-2:]
if v1 == '01':
res.append(i[:4] + '-03-31')
elif v1 == '02':
res.append(i[:4] + '-06-30')
elif v1 == '03':
res.append(i[4:] + '-09-30')
else:
res.append(i[4:] + '-12-31')
#>>> res
#['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']
One way is to keep treating it as a string, and simply do an if / else statement, i.e.
res =
for i in df['Date']:
v1 = i[-2:]
if v1 == '01':
res.append(i[:4] + '-03-31')
elif v1 == '02':
res.append(i[:4] + '-06-30')
elif v1 == '03':
res.append(i[4:] + '-09-30')
else:
res.append(i[4:] + '-12-31')
#>>> res
#['2016-03-31', '2016-06-30', '03-09-30', '04-12-31']
answered Nov 19 at 14:42
Sotos
26.9k51540
26.9k51540
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%2f53376598%2fconvert-dataframe-to-quarter%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