Python Forum
future warning when using numpy fancy indexing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
future warning when using numpy fancy indexing
#1
After loading arrays from a data file, I get a future warning message when I try and use the result of one of the arrays as a mask over another;

this generates the same error, but uses sample data instead of a file:
#!/usr/bin/python3
import sys
import numpy as np
import os.path as op
from datetime import datetime, date, time
from io import StringIO

sample_data = StringIO(
"AAPL,28-01-2011,344.17,344.4\n\
AAPL,31-01-2011,335.8,340.04\n\
AAPL,01-02-2011,341.3,345.65\n\
AAPL,02-02-2011,344.45,345.25\n\
AAPL,03-02-2011,343.8,344.24\n\
AAPL,04-02-2011,343.61,346.7")

def usage():
    print("usage: {} {}".format(op.basename(sys.argv[0], 'filename')))

def get_weekday(date_str):
    return datetime.strptime(date_str.decode('ascii'), "%d-%m-%Y").date().weekday()

def load_arrays(data_file, *col_tuple):
    a1 = a2 = a3 = None

    try:
        a1, a2, a3 = np.loadtxt(data_file, 
                                dtype={'names': ('stock_code','cob_date','close_price'),
                                       'formats': ('S4', 'S10', 'f4')}, 
                                converters={1: get_weekday}, delimiter=',',
                                usecols=col_tuple, unpack=True)

    except IOError as e:
        usage() # failed to open file
    except Exception as e: print(e)

    return a1, a2, a3

try:
    data_file = sample_data
    s, d, c= load_arrays(data_file, 0,1,3)
except IndexError:
    usage()

if __name__ == "__main__":
    print("Stock codes:\n{}".format(s))
    print("Day of the week:\n{}".format(d))
    print("Closing price:\n{}".format(c))

    indicies = np.where(np.array(d) == 1) # future warning generated on this line
    print(f"Indicies\n{indicies}")
As you can see the indicies array has no elements:
/week_day_avg_price.py:57: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  indicies = np.where(np.array(d) == 1)
Indicies
(array([], dtype=int64),)
When I use a filter the message does not appear, but the array is still empty.
with warnings.catch_warnings():
    warnings.simplefilter(action='ignore',category=FutureWarning)
so the question is, how can I get the indicies based on the week day ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 544 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,604 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Question about Numpy indexing. water 1 1,453 Jan-18-2022, 09:52 PM
Last Post: paul18fr
  Predicting/Forecasting Future Values BadWhite 1 2,358 Jun-15-2020, 11:30 AM
Last Post: hussainmujtaba
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,976 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 4,188 Mar-20-2018, 08:29 PM
Last Post: bentaz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020