datetime Attribute error in python - python-3.5

I'm trying to run to following three lines of python code on command line using Python 3.5.0. It gives me an error- Attribute error: module 'datetime' has no attribute 'date'. I just want to print current date. Please help.
import datetime
current = datetime.date.today()
print(current)

There is nothing wrong with your code. It could be reduced a bit though:
import datetime
datetime.date
which should also cause the error. If this really causes the error, I would say your installation is messed up or, unlikely, there's a bug in Python. Please also make sure you don't have a datetime.py in your working directory. Further, check the output of dir(datetime) after importing it and with a different version of Python.

You shouldn't be getting any error running the above code as there is nothing wrong with it. Also rather than using the above code (which is okay syntax-wise but imports all the names accessible in the datetime moudule), you could use
from datetime import date
current = date.today()
print(current)
since all you want to import is the day's date.

when i run it on python 27. the code returns date with no errors!

Related

Quantstats - TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime

I'm trying to use some of the Quantstats modules, specifically the quantstats.reports module, in Anaconda to get some metrics reports on a portfolio I've designed. I'm fairly new to Python/Quantstats and am really just trying to get a feel for the library.
I've written the following code to utilize the report module to spit out a complete html report and save it under the Output folder:
import quantstats as qs
qs.extend_pandas()
stock = qs.utils.download_returns('GLD')
qs.reports.html(stock, output='Output/GLD.html')
I then get the following TypeError:
TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime
I believe this may be a result of the datetime64 class being localized to my timezone and datetime remaining TZ naive. Frankly, digging through the Quantstats code has been a little beyond my current skillset.
If anybody has any recommendations for fixes, I would greatly appreciate it.
I came upon this while DDGing exactly the same issue.
Not sure which of your columns has the timezone localisation in it, but
df['date'] = df['date'].dt.tz_localize(None)
will get rid of localization for the column df['date']
Incidentally, the usual situation is that the index of a pandas timeseries contains np.Datetime64 types, but when you assign it to a column via
df['date'] = df.index
the resulting column contains pandas Timestamps.
I got this issue resolved after I had lowered the yfinance version from latest version 0.1.87 to
yfinance => 0.1.74

Python 3 - datetime Class/module Error in VS Code (Windows)

I am trying to print a simple statement that will show today's date. But no matter how I try, I keep getting the same error. Here are some examples:
import datetime
today = datetime.datetime.date.today()
print(today)
I've also tried:
from datetime import datetime
today = datetime.date.today()
print(today)
Either way, this is the error message I get in the terminal:
"AttributeError: partially initialized module 'datetime' has no attribute 'date' (most likely due to a circular import)"
Any advice? I'm going nuts and yet, it should be so simple! #_#
Your file name is the same as the package:
Change it for antoher name, like 'Time_example.py'

Catching Import errors and Name errors in Python during "compile" time

Can you catch import / name and other errors in python using a (linting) tool or a compilation step?
The other option is to make sure all possible code paths are tested (This is not always feasible, especially for large existing code bases and other reasons)
Here are some examples.
Missing import - caught by pylint, although as a syntax error instead of an import error.
def test():
print("Time now is ..", datetime.datetime())
pylint output:
E0602: Undefined variable 'datetime' (undefined-variable)
Import present, but incorrect method used. This passes both pylint and py_compile.
from datetime import datetime
def test():
print("Time now is ..", datetime.today2())
Edit: To add one more option.
Doing an import * shows some errors, but not errors in statements which are inside the functions.
This error is reported
from datetime import datetime
print("today2", datetime.today2())
Error :
Python 3.7.0 (default, Aug 22 2018, 15:22:56)
>>> from test import *
...
print("today2", datetime.today2())
AttributeError: type object 'datetime.datetime' has no attribute 'today2'
>>>
This is not.
from datetime import datetime
def test():
print("Time now is ..", datetime.today2())
In my experience, flake8 does a great job of catching missing imports and name errors. In order to catch missing imports, you must not use wildcard imports like "from foo import *", since it cannot guess which names that will create. Also, it cannot do these detections while syntax errors exist, so you have to fix those first.
Unlike c++ which is a compiled language, python is an interpreted language. Which means it doesn't have compile phase. It interpreted code line by line.
According to that, you didn't find the errors until you reach them in runtime.
If you want that errors appears, you should somehow path throw every line of your code. The best approach is using test libraries with 100% test coverage.
For more information look at this question and it's answers.

Rodeo giving error on Excel import working in Spyder

Full disclosure: I am a total beginner when it comes to Python in particular and programming in general. So please bear with me.
Today I tried for the first time to play around some datasets on my own, outside of the sandboxed environment of online courses.
I downloaded both Anaconda and Rodeo (which somehow I feel more akin to than, say, Spyder or Jupyter).
Wrote down this code. It works in Spyder.
import numpy as np
import pandas as pd
myexcel="C:/Users/myname/folder/subfolder/file.xlsx"
xl=pd.ExcelFile(myexcel)
mydf=xl.parse(0)
print(mydf.head())
However, if I try to run the same code in Rodeo I get the following error message. Here, I am showing just a part.
----> 4 xl=pd.ExcelFile(myexcel)
ImportError: No module named 'xlrd'
I am getting that in Rodeo the script fail because it is missing the xlrd package, which admittedly after checking with help("modules") is not there. But I don't fully get the problem: if xlrd was quintessential to the correct execution of this code, then why doesn't it fail in Spyder?

cannot use LOCALE flag with a str pattern

import xlwt
wb = xlwt.Workbook()
sheet1 = wb.add_sheet('Sheet 1')
wb.save('self, example.xls')
I am trying to learn how to create xls, edit xls, or delete if neccesary on python. I had verymuch trouble on this because every tutorial online doesn't mentions that I should put xlwt before Workbook but I figure it out now. The problem is when I run this code I get an error wich it says "ValueError: cannot use LOCALE flag with a str pattern" I don't even know what that means... What is it about and how can I fix it?
There is a known-issue with tablib and Python 3.6, looks like it will be solved in the next releases.
For now, I make it work just downgrading to python 3.5.2
I had a similar problem and i resolved it by changing my series to "str" data type. df.astype("str")
Also not sure why you are trying to save your file as "self, example.xls". Could you print the entire error as it was given.

Resources