I have a large file that contains a table. I am going to open it with the datatable
package in python. Consider the following code
import datatable as dt
from datatable import f
df = dt.open('Mybigfile.nff')
df = df[(f.c1 > 0) & (f.c1 < 100),:]
print(df.shape)
when I run it like python code.py
where code.py
contains the above code, everything is fine.
But when I put the code in an flask-api I get the error
AttributeError: '_io.TextIOWrapper' object has no attribute 'c1'
from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS
from json import dumps
import datatable as dt
from datatable import f
app = Flask(__name__)
CORS(app)
api = Api(app)
class test(Resource):
def get(self):
df = dt.open('Mybigfile.nff')
df = df[(f.c1 > 0) & (f.c1 < 100),:]
print(df.shape)
return 1
api.add_resource(test, '/gettest')
if __name__ == '__main__':
app.run(host= '0.0.0.0',port=12345)
It seems the error comes from expression (f.c1 > 0)
. For some reason (and I don't know how it happens), your f
variable is not the object imported from datatable
, but rather an object of class _io.TextIOWrapper
.
The problem can be resolved, however, with one of these workarounds:
Import f
under a different name, for example from datatable import f as F
. Then you'll write df[(F.c1 > 0) & (F.c1 < 100), :]
.
Use f
directly from the dt
namespace: df[(dt.f.c1 > 0) & (dt.f.c1 < 100), :]
.
Finally, you can try to find out how f
became a TextIOWrapper in the first place. You probably have a code fragment somewhere that reads with open(...) as f: ...
. When that piece is executed, f
becomes redefined.