0

So datestr() is supposed to convert a number into a date. But I keep getting this Name error message. Am I not loading the correct module. I have searched the Matplotlib documenation but do not see any specific module that must be imported.

import matplotlib.pyplot as plt
    from matplotlib.dates import DateFormatter, WeekdayLocator,\
        DayLocator, MONDAY
    from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
    import pandas as pd
    import datetime
    import pandas.io.data as web
    from datetime import date
    import matplotlib

    date = 731613

    print datestr(date)
    #NameError: name 'datestr' is not defined
  • What makes you think this is a built-in python function? Sounds more like matlab to me. In python you would use something from [here](https://docs.python.org/2/library/datetime.html) or some wrappers in pandas/numpy/matplotlib. – sascha Aug 14 '16 at 22:36
  • In all of the examples used http://www.mathworks.com/help/matlab/ref/datestr.html I don't see where they importa a certain module. I have already imported some I plan to use. – Jonathan Holloway Aug 14 '16 at 22:39
  • Why are you referring to matlab's docs when using python? You know that these are two different languages/environments? – sascha Aug 14 '16 at 22:40

2 Answers2

1

It looks like you want the function mathplotlib.dates.num2date(). From there you can convert to a string with str() or strftime():

>>> from matplotlib.dates import num2date
>>> num2date(731613)
datetime.datetime(2004, 2, 2, 0, 0, tzinfo=<matplotlib.dates._UTC object at 0x7f64861fa5d0>)
>>> print(num2date(731613))
2004-02-02 00:00:00+00:00
>>> str(num2date(731613))
'2004-02-02 00:00:00+00:00'
mhawke
  • 84,695
  • 9
  • 117
  • 138
0

Yes I was not using the correct function. I did a search on the problem and I ran into the soltution I tried above the correct format is below.

from datetime import date
dte = 731613
print date.fromordinal(dte)
#out put is >> 2004-02-02 
#          (Year, Month, day)