Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with datetime #4557

Closed
Gurdil opened this issue Jun 26, 2015 · 15 comments
Closed

Problem with datetime #4557

Gurdil opened this issue Jun 26, 2015 · 15 comments

Comments

@Gurdil
Copy link

Gurdil commented Jun 26, 2015

from matplotlib import pyplot as plt
from datetime import datetime

datetime.strptime("Sep", '%b')

throw an exception :
Traceback (most recent call last):
File "/home/workspace/python/ephemeride/t.py", line 10, in
datetime.strptime("Sep", '%b')
File "/usr/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.4/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data 'Sep' does not match format '%b'

The exception doesn't occur if the matplotlib is not imported.
python 3.4.2/Ubuntu 14.10

@tacaswell tacaswell added this to the unassigned milestone Jun 26, 2015
@tacaswell
Copy link
Member

I can not reproduce this.

dd_3k) ✔ ~ 
11:57 $ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun  4 2015, 15:29:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.strptime("Sep", '%b')
datetime.datetime(1900, 9, 1, 0, 0)
>>> 
>>> 
(dd_3k) ✔ ~ 
11:57 $ 
(dd_3k) ✔ ~ 
11:57 $ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun  4 2015, 15:29:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot as plt
>>> from datetime import datetime
>>> datetime.strptime("Sep", '%b')
datetime.datetime(1900, 9, 1, 0, 0)
>>> 

Does

>>> import datetime
>>> datetime.__file__

give you the same result in both cases?

what version of mpl are you using and how did you install it?

@WeatherGod
Copy link
Member

perhaps this is a locale issue?

On Fri, Jun 26, 2015 at 12:03 PM, Thomas A Caswell <notifications@github.com

wrote:

I can not reproduce this.

dd_3k) ✔ ~
11:57 $ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun 4 2015, 15:29:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

from datetime import datetime
datetime.strptime("Sep", '%b')
datetime.datetime(1900, 9, 1, 0, 0)

(dd_3k) ✔ ~
11:57 $
(dd_3k) ✔ ~
11:57 $ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun 4 2015, 15:29:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
from matplotlib import pyplot as plt
from datetime import datetime
datetime.strptime("Sep", '%b')
datetime.datetime(1900, 9, 1, 0, 0)

Does

import datetime
datetime.file

give you the same result in both cases?

what version of mpl are you using and how did you install ite?


Reply to this email directly or view it on GitHub
#4557 (comment)
.

@Gurdil
Copy link
Author

Gurdil commented Jun 29, 2015

I install matplotlib through pip3

datetime.__file__

return in both case :
/usr/lib/python3.4/datetime.py

import matplotlib
print(matplotlib.__version__)
print(matplotlib.__file__)
print(matplotlib.get_configdir())

return :
1.4.3
/usr/local/lib/python3.4/dist-packages/matplotlib/init.py
/home/philip_j/.config/matplotlib

@tacaswell
Copy link
Member

What is the native locale of your machine?

@Gurdil
Copy link
Author

Gurdil commented Jun 29, 2015

I'm not sure it's respond to the question but the command :

$ locale

return :

LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=fr_FR.UTF-8
LC_TIME=fr_FR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=fr_FR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=fr_FR.UTF-8
LC_NAME=fr_FR.UTF-8
LC_ADDRESS=fr_FR.UTF-8
LC_TELEPHONE=fr_FR.UTF-8
LC_MEASUREMENT=fr_FR.UTF-8
LC_IDENTIFICATION=fr_FR.UTF-8
LC_ALL=

@WeatherGod
Copy link
Member

On Mon, Jun 29, 2015 at 11:27 AM, Jean-Baptiste PHILIPPE <
notifications@github.com> wrote:

LC_TIME=fr_FR.UTF-8

Could you try the french word for September? LC_TIME indicates that it is
set to French.

@tacaswell
Copy link
Member

Do the French month abbreviations work after you import matplotlib? My guess as to what is happening is that mpl is inspecting the LC envs and updating something on the python side.

Can you get the same error from just importing dateutil?

@Gurdil
Copy link
Author

Gurdil commented Jun 30, 2015

I test with 'Sep' because I assume it's the same in french(Septembre) and English(September).
The doc seem to only make an exception for Dutch:
https://docs.python.org/3.4/library/datetime.html#strftime-and-strptime-behavior

dateutil seem to work fine:

>>> from matplotlib import pyplot as plt
>>> from dateutil.parser import *
>>> print(parse("Sep"))
2015-09-30 00:00:00

@tacaswell
Copy link
Member

The documentation reads to me like it is just using an example for Dutch.

What does

datetime.strptime("Sep", '%b')
import locale
datetime.strptime("Sep", '%b')

do on your system?

@tacaswell
Copy link
Member

This looks like things working correctly, but not in the way you expect.

In [1]: import locale                                  

In [2]: from datetime import datetime                  

In [3]: datetime.strptime("Sep", '%b')         
Out[3]: datetime.datetime(1900, 9, 1, 0, 0)

In [4]: locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8')
Out[4]: 'fr_FR.UTF-8'

In [5]: datetime.strptime("Sep", '%b')                 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-2fa518be5c22> in <module>()
----> 1 datetime.strptime("Sep", '%b')

/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py in _strptime_datetime(cls, data_string, format)
    498     """Return a class cls instance based on the input string and the
    499     format string."""
--> 500     tt, fraction = _strptime(data_string, format)
    501     tzname, gmtoff = tt[-2:]
    502     args = tt[:6] + (fraction,)

/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py in _strptime(data_string, format)
    335     if not found:
    336         raise ValueError("time data %r does not match format %r" %
--> 337                          (data_string, format))
    338     if len(data_string) != found.end():
    339         raise ValueError("unconverted data remains: %s" %

ValueError: time data 'Sep' does not match format '%b'

(to get this to run I had to generate the French locale files on my system)

It looks like in the top-level __init__.py there is a check if the rcparam 'axes.formatter.use_locale' is true (https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/__init__.py#L1126) which calls locale.setlocale(local.LC_ALL, '') which queries the system to get the user set locale (https://docs.python.org/3.4/library/locale.html#locale.setlocale). By default this rcparam is False, my guess is you have this set to True on your machine?

ex

12:20 $ LC_TIME=fr_FR.UTF-8 python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun  4 2015, 15:29:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.         
>>> from datetime import datetime
>>> datetime.strptime("Sep", '%b')
datetime.datetime(1900, 9, 1, 0, 0)
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'LC_CTYPE=en_US.UTF-8;LC_NUMERIC=en_US.UTF-8;LC_TIME=fr_FR.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=en_US.UTF-8;LC_ADDRESS=en_US.UTF-8;LC_TELEPHONE=en_US.UTF-8;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=en_US.UTF-8'
>>> datetime.strptime("Sep", '%b')     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data 'Sep' does not match format '%b'
>>> 

If I set that rcparam to True I can reproduce this:

(dd_3k) ✔ ~ 
12:26 $ LC_TIME=fr_FR.UTF-8 python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Jun  4 2015, 15:29:08) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.strptime("Sep", '%b')     
datetime.datetime(1900, 9, 1, 0, 0)
>>> import matplotlib
>>> datetime.strptime("Sep", '%b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/home/tcaswell/.virtualenvs/dd_3k/lib/python3.4/_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data 'Sep' does not match format '%b'
>>> 
>>> datetime.now().strftime('%b')
'juil.'
>>> 

I do not think this is a bug, I think this is the system behaving in the intended way (even if that way is a bit annoying). Note the flip side of this is that you get dates printed out in the locale you have set.

If you need to be able to parse dates in a given locale, see http://stackoverflow.com/questions/18593661/how-do-i-strftime-a-date-object-in-a-different-locale

@tacaswell
Copy link
Member

Sorry that last message is rather rambling, I was sorting out out while I was writing.

@Gurdil
Copy link
Author

Gurdil commented Jul 2, 2015

Thank, now I understand.

from matplotlib import pyplot as plt
from datetime import datetime

for i in range(1,13):
    date = datetime.strptime(str(i), '%m')
    print(date.strftime('%b'))

give me my local month abbreviation:

janv.
févr.
mars
avril
mai
juin
juil.
août
sept.
oct.
nov.
déc.

But the rcparam 'axes.formatter.use_locale' don't seem to be guilty, here a dump of my rcparam :
https://gist.github.com/Gurdil/c25e4f4b46ed4ace4964

Anyway thank you for helping me to solve this problem and I learned some things about python. It's a good days.

@tacaswell
Copy link
Member

Hm, something is using locale but I am not sure it is worth tracking down.

Closing this as there isn't a bug in MPL, just tricksy internationalization.

@Kangomnawe
Copy link

Capture

Anyone pls..

@tacaswell
Copy link
Member

@Kangomnawe That should probably be reported against pandas as Matplotlib does not appear to be involved on what you posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants