2

The 'Date' column in the dataframe is like this:

19 Mar. 2017
12 Mar. 2017
08 Mar. 2017
05 Mar. 2017
26 Feb. 2017
19 Feb. 2017
12 Feb. 2017
05 Feb. 2017
29 Jan. 2017
22 Jan. 2017
15 Jan. 2017
08 Jan. 2017
01 Jan. 2017
25 Dec. 2016
18 Dec. 2016
11 Dec. 2016
04 Dec. 2016
27 Nov. 2016
20 Nov. 2016

When I tried to do:

df = df[pd.to_datetime(df['Date']).dt.year==2016]

It complained:

ValueError: Unknown string format

I think it cannot recognize the date string format.

Any idea how to solve this?

2 Answers 2

2

You can specify format with pd.to_datetime for non-standard formats, e.g. pd.to_datetime(my_series, format='%d %b. %Y'). Then use pd.Series.dt.year. This is the recommended method. See also Python's strftime directives.

However, if you are only converting to datetime to filter by year and not changing the dtype of your series, you can just query the last 4 characters:

df[df['dates'].str[-4:] == '2016']
Sign up to request clarification or add additional context in comments.

Comments

1

Use custom format by parameter format in to_datetime:

df['Date'] = pd.to_datetime(df['Date'], format='%d %b. %Y')
df = df[df['Date'].dt.year==2016]
print (df)
         Date
13 2016-12-25
14 2016-12-18
15 2016-12-11
16 2016-12-04
17 2016-11-27
18 2016-11-20

1 Comment

there are two sorts of formats in the 'Date' column:27 Nov. 2016 and 28 May 2018

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.