So, I'm building a model and trying to use | for a new column and I'm confused as to what I'm doing wrong. It's supposed to make a new column and type 1 when the value is true.
For example, this worked :
feature_matrix["After 2016"] = (feature_matrix.index.year > 2016).astype(int)
However this does nothing :
feature_matrix["Summer"] = (feature_matrix.index.month == 6|7|8|9).astype(int)
Same thing goes for this when I try to do the weekend using the same method.
I tried solving it using :
feature_matrix["Summer"] = (feature_matrix.index.month == 6| feature_matrix.index.month == 7).astype(int)
But that gives me : unsupported operand type(s) for |: 'int' and 'Int64Index'
We have isin
(feature_matrix.index.month.isin([6,7,8,9]))
Yoben's answer is correct and complete, but a note: 6|7|8\9 is always true, and unless you have True in your column, this will do nothing.
Related
Im having issues understand what arguments to send to the growattServer 1.4.0 function api.dashboard_data()
The function looks like
def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None):
class Timespan(IntEnum):
hour = 0
day = 1
month = 2
If I try
print(api.dashboard_data('INVERTERID',timespan=0,date='2023-02-01'))
I get:
TypeError: unsupported operand type(s) for 'in': 'int' and 'EnumType'
I get the same error if I do
print(api.dashboard_data('INVERTERID',0,date='2023-02-01'))
I do not understand what arguments to pass for the timespan function and would appricate some help with this.
For timespan you are passing in a 0 -- in both attempts; includeing the parameter name makes no difference.
Try:
print(api.dashboard_data('INVERTERID', Timespan.hour, date='2023-02-01')
Questions for you:
can plant_id be a string?
can date be a string? Or should it be a datetime.date?
def function1(self,*windows):
xxx_per_win = [[] for _ in windows]
for i in range(max(windows),self.file.shape[0]):
for j in range(len(windows)):
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
...
...
...
o = classX(file)
windows = [3,10,20,40,80]
output = o.function1(windows)
If I run the code above it says:
for i in range(max(windows),self.file.shape[0]):
TypeError: 'list' object cannot be interpreted as an integer
and:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
This problem only occurs when windows is variable length (ie *windows and not just windows).
How do I fix this? What is causing this?
The max function expects to be passed multiple arguments, not a tuple containing multiple arguments. Your windows variable is a tuple.
So, not:
for i in range(max(windows),self.file.shape[0]):
Do this instead:
for i in range(max(*windows),self.file.shape[0]):
On your second error, involving the line:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
# TypeError: unsupported operand type(s) for -: 'int' and 'list'
Ok, you are subtracting, and it's complaining that you can't subtract a list from an integer. And since I have no idea what windows[j] contains, I can't say whether there's a list in there or not.. but if there is, there can't be. You haven't given us a working example to try.
What I suggest you do is to put some debugging output in your code, eg:
print('i=%s, j=%s, windows[j]=%s' % (i, j, windows[j]))
and thus see what your data looks like.
ctx['location_ids'] = vals['location_ids']
I have a large function so I will not post it here, but the problem is when vals['location_ids'] have values as integer everything works smooth, but sometimes there are no values in vals['location_ids'] so it is False, and when it is False I get error.
ctx['location_ids'] = vals['location_ids']
TypeError: 'bool' object has no attribute '__getitem__'
how can I avoid it, maybe add hasattr?
you should try to check first it's dictionary
if isinstance(vals, dict):
ctx['location_ids'] = vals.get('location_ids', None)
I'm seeing weird behavior on this code:
images = dict(cover=[],second_row=[],additional_rows=[])
for pic in pictures:
if len(images['cover']) == 0:
images['cover'] = pic.path_thumb_l
elif len(images['second_row']) < 3:
images['second_row'].append(pic.path_thumb_m)
else:
images['additional_rows'].append(pic.path_thumb_s)
My web2py app gives me this error:
if len(images['cover']) == 0:
TypeError: object of type 'NoneType' has no len()
I can't figure out what's wrong in this. Maybe some scope issue?
You assign something new to images['cover']:
images['cover'] = pic.path_thumb_l
where pic.path_thumb_l is None at some point in your code.
You probably meant to append instead:
images['cover'].append(pic.path_thumb_l)
your problem is that
if len(images['cover']) == 0:
checks the LENGTH of the value of images['cover'] what you meant to do is check if it HAS a value.
do this instead:
if not images['cover']:
The first time you assign: images['cover'] = pic.path_thumb_l, it replaces the value of the empty list initially stored in images['cover'] with the value of pic.path_thumb_l which is None.
Maybe your code in this line must be images['cover'].append(pic.path_thumb_l)
We can also see the type in the same condition, to avoid something if you want, like
if myArray is None:
#Do something when array has no len()
else:
#Do something when array has elements and has len()
In my case I was looking for something in the array, but only if has something, when id does not, was None the type and I need to create it. Hope this works for someones.
In python I get this error:
TypeError: 'int' object is unsubscriptable
This happens at the line:
sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
I couldn't find a good definition of unsubscriptable for python anywhere.
for quote in sector[singlestock]:
i+=1
if i < len(sector):
if i==0:
sectorcalc[i][0]= quote[0]
sectorcalc[i][2]= 0
sectorcalc[i][3]= 0
sectorcalc[i][4]= 0
sectorcalc[i][5]= 0
sectorcalc[i][6]= 0
sectorcalc[i][7]= 0
else:
yesterday = sector[singlestock-1][i]
print yesterday
today = quote
print type(today[2])
sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
sectorcalc[i][3]= (today[3]/yesterday[3])-1
sectorcalc[i][4]= (today[4]/yesterday[4])-1
sectorcalc[i][5]= (today[5]/yesterday[5])-1
sectorcalc[i][6]= (today[6]/yesterday[6])-1
sectorcalc[i][7]= (today[7]/yesterday[7])-1
What does this error mean?
The "[2]" in today[2] is called subscript.
This usage is possible only if "today"
is a sequence type. Native sequence
types - List, string, tuple etc
Since you are getting an error - 'int' object is unsubscriptable. It means that "today" is not a sequence but an int type object.
You will need to find / debug why "today" or "yesterday" is an int type object when you are expecting a sequence.
[Edit: to make it clear]
Error can be in
sectorcalc[i]
today (Already proved is a list)
yesterday
This is confusing to read:
today = quote
Is today = datetime.date.today()? Why would a date suddenly refer to a quote? Should the variable name be quoteForToday or something more expressive? Same for yesterday. Dividing two dates as you do makes no sense to me.
Since this is a quote, would today and yesterday refer to prices or rates on different days? Names matter - choose them carefully. You might be the one who has to maintain this six months from now, and you won't remember what they mean, either.
Not that the code you wrote is valid, but I can't see why you wouldn't use a loop.
for j in range(2,7):
sectorcalc[i][j] = (today[j]/yesteday[j])-1
instead of
sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
sectorcalc[i][3]= (today[3]/yesterday[3])-1
sectorcalc[i][4]= (today[4]/yesterday[4])-1
sectorcalc[i][5]= (today[5]/yesterday[5])-1
sectorcalc[i][6]= (today[6]/yesterday[6])-1
sectorcalc[i][7]= (today[7]/yesterday[7])-1
How to reproduce that error:
myint = 57
print myint[0]
The people who wrote the compiler said you can't do that in the following way:
TypeError: 'int' object is unsubscriptable
If you want to subscript something, use an array like this:
myint = [ 57, 25 ]
print myint[0]
Which prints:
57
Solution:
Either promote your int to a list or some other indexed type, or stop subscripting your int.