468-4 Wrong expected type of 'yearmonth' field

Screen Link: https://app.dataquest.io/m/468/business-metrics/4/net-promoter-score

Your Code:

I copy your solution lines, including
df['yearmonth'] = df['event_date'].dt.to_period('M')

What I expected to happen:

yearmonth field in the form YYYYMM as stated by the autograder (or at least it is printed in this from).

What actually happened:

yearmonth is formatted as YYYY-MM. Pandas docs also show this format of output for to_period('M').

Other details:

I tried df['yearmonth'] = df['event_date'].astype('str').str.replace('-', ''), but it still fails. Looks like the autograder also checks dtype for this column. What is the expected dtype?

Hey, Pavel.

Apologies for this. This has been half-fixed since you asked this. The instruction now reads as follows.

Create a numeric column in df called yearmonth that stores the year and the month of each row in the yyyy-mm format.

I said half-fixed above, because there’s a typo: the format should be yyyymm. With this in mind, you should be able to now complete this screen.

In case you don’t want to invest any more time on this screen, you seem to have a great grasp of what is going on, so feel free to copy and paste the solution (which I leave below), if you prefer.

import pandas as pd

df = pd.read_csv("nps.csv", parse_dates=["event_date"])
year = df["event_date"].dt.year
month = df["event_date"].dt.month
df["yearmonth"] = 100*year + month

df["category"] = df["score"].apply(categorize)

nps = df.pivot_table(index="yearmonth", columns="category", aggfunc="size")
nps["total_responses"] = nps.sum(axis="columns")
nps["nps"] = (nps["Promoter"]-nps["Detractor"])/nps["total_responses"]
nps["nps"] = (100*nps["nps"]).astype(int)
1 Like

Thank you, Bruno!

A number! So obvious in hindsight…

2 Likes