问题背景
书上的一个例子
import pandas.util.testing as tm
colors = tm.choice(['red','green'],size=10)
运行结果:
但我运行就报错:
module 'pandas.util.testing' has no attribute 'choice'
我查了一下pandas.util.testing包里面好像的确没有choice()函数。。我也不知道当初书上那个是怎么做出来的。我看官方好像提供了一个函数randu_array(),但我用
colors = tm.randu_array(['red','green'],size=10)
报错:
cannot perform reduce with flexible type
报了一长串的错,只是最后一句,
问题更新
底下的二楼大佬回复我说可以这样
import numpy as np
import pandas.util.testing as tm
tm.np.random.choice()
我试了一下可以!而且我是知道numpy.random里面有个choice()函数的,
不过为什么tm可以引用到np这个名称?
testing
只是把一些常用的功能集中到一块而已~它原来的choice()就是 numpy.random里面的choice()
你可以去看
testing
的源文件,它导入numpy ,所以可以引用到np这个名称