0
d1 = today.strftime("%b-%y")
print("d1 =", d1)

d1 = ene.-21

But the variable I want to convert in my data set is Jan-21.

My code

data['date_text_DATE'] = pd.to_datetime(data['date_text'], format = '%b-%y')

The error I got:

ValueError: time data 'Jan-21' does not match format '%b-%y' (match)

It does not convert the text into date format.

1 Answers1

0

EDIT: author noted in comments below the issue was due to their Python/R environment.

I'm not sure what you're trying to accomplish with the d1 variable but the sample I created works fine for me.

I suspect your column has a value within it not conforming to a date pattern. Can you share the whole dataset or that column

Here is what I did.

df = pd.DataFrame(data={'a':'Jan-21'}, index=[0])
pd.to_datetime(df['a'], format='%b-%y')

And it worked fine.

You might want to check your encoding isn't doing something weird, maybe Cyrillic alphabet or something. The following should return 425: sum([ord(l) for l in 'Jan-21'])

Where 'Jan-21' should be your cell value from the Pandas Dataframe and not, obviously, what I just typed above.

EDIT 2: More details regarding the issue, from comments, that helped OP.

df['a'].dt.strftime(date_format='%b-%Y') will convert to datetime any values of the format mmm-yy. All pandas series have a .dt accessor which gives you a lot of datetime related functions such as being able to return .weekofyear or .second, and .strftime (string format time) is a very common Python method which allows the string formatting of a datetime utilising the format style you require. You can change the dash for forward-slash, etc. You can see all the format options at this link: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

More on .dt accessor: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html

ALSO

If you are getting errors with datetime and it seems absolutely sure you are doing the correct thing (such as the example given in Edit 2) then reinstall and upgrade your datetime module by doing, in order:

  1. pip uninstall datetime
  2. pip install datetime
  3. pip install datetime --upgrade
Paul Wilson
  • 562
  • 5
  • 16
  • Hello Paul. Thanks for your reply. d1 is an example to show you how my IDE recognizes "today"s date, in Spanish indeed. While the string i want to convert is in English. – Karlos Garcia Jan 28 '21 at 18:24
  • Hi Karlos. Can you please provide some example data in the format you are working with? Thanks. – Paul Wilson Jan 29 '21 at 21:58
  • date = ["Jan-85", "Jan-93", "Jan-00"] – Karlos Garcia Jan 30 '21 at 13:22
  • It works absolutely fine for me. You must be doing something else that either you are unaware of or are not stating here. Unless you share your entire code then I'm sorry I don't think anyone can help. Here is what I tried which worked fine. `df = pd.DataFrame(data={'a':["Jan-85", "Jan-93", "Jan-00"]}) df['a'] = pd.to_datetime(df['a'], format='%b-%y')` EDIT: Make sure your datetime library is up to date. Run `pip install datetime` and then `pip install datetime --upgrade` in your terminal to do this. Then try again. That's my last ditch attempt to help, I can't think of anything else.. – Paul Wilson Jan 30 '21 at 17:21
  • Hi Paul. I am using Rstudio and the python environment was created with python 3.8 and the datetime (package https://anaconda.org/trentonoliphant/datetime) is updated untill 3.4. I am going to create another env. with 3.4 and see if it workds. – Karlos Garcia Jan 31 '21 at 19:24
  • Hi Paul. It worked :-) Thanks for caring. But I am still curious how the geographical location (i am in Spain) affect the date type variables manipulation – Karlos Garcia Jan 31 '21 at 20:18
  • Hi Karlos, I'm glad that it worked! please accept my answer as correct and I will edit it to show people it was an environment issue. Your location should not impact things, as far as I know all Europeans (I am European too) use the day-month-year format and so the format should make no difference to you or me. I believe the issue was your Python/R environment. – Paul Wilson Feb 01 '21 at 01:04
  • Hi Paul. The result I get is the following is: date = ["1985-01-01", "1993-01-01", "2000-01-01"], which is good because I have a date format: "", but it includes the day and the month as number. I thought that indicating the format ('%b-%y') would work, but actually, it does not. It looks like is not picking up the format. – Karlos Garcia Feb 01 '21 at 21:11
  • Here you are. `df['a'].dt.strftime(date_format='%b-%Y')`. All pandas series have a `.dt` accessor which gives you a lot of datetime related functions such as being able to return `.weekofyear` or `.second`, and `strftime` (string format time) is a very common Python method which allows the string formatting of a datetime utilising the format style you require. You can change the dash for forward-slash, etc. You can see all the format options at this link: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes – Paul Wilson Feb 02 '21 at 00:47
  • The pandas documentation for the above function, where you can see all .dt. functions, is here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html – Paul Wilson Feb 02 '21 at 00:48
  • Hi Paul. I would mention the Python version, I think the datetime is not updated for the latest Python versions – Karlos Garcia Feb 07 '21 at 22:04
  • Hopefully what I mentioned about upgradinig datetime is sufficient? – Paul Wilson Feb 09 '21 at 23:24
  • I do not think so because datetime is not updated with the latest Python versions – Karlos Garcia Feb 27 '21 at 20:32