在这段Python代码中.N是什么意思?

在这段Python代码中.N是什么意思?,python,Python,我正在努力学习Python,并在继续之前浏览GitHub上的一些代码。我只是好奇.N在带有“tm.N=1000”的行上做了什么,以及它与代码结尾的关系 import matplotlib.pyplot as plt import random import pandas.util.testing as tm tm.N = 1000 df = tm.makeTimeDataFrame() import string foo = list(string.letters[:5]) * 200 df[

我正在努力学习Python,并在继续之前浏览GitHub上的一些代码。我只是好奇.N在带有“tm.N=1000”的行上做了什么,以及它与代码结尾的关系

import matplotlib.pyplot as plt

import random
import pandas.util.testing as tm
tm.N = 1000
df = tm.makeTimeDataFrame()
import string
foo = list(string.letters[:5]) * 200
df['indic'] = list(string.letters[:5]) * 200
random.shuffle(foo)
df['indic2'] = foo
df.boxplot(by=['indic', 'indic2'], fontsize=8, rot=90)

plt.show()

前一行,
import pandas.util.testing as tm
,导入模块
pandas.util.testing
,为方便起见,将其命名为
tm
。因此,
tm
之后指的是该模块,因此
tm.N
指的是模块中名为“
N
(无论是什么)的对象。

来源:


N是pandas.util.testing库中的一个变量(作为
tm
导入)。它在该库中定义的一些函数中使用,包括在
getTimeSeriesData
中调用的
makeTimeSeries
函数,该函数依次在
makeTimeDataFrame
函数中调用,您使用
df=tm.makeTimeDataFrame()调用该函数

您可以从和函数中获取有关pandas.util.testing.N的信息:

>>> tm.N.__doc__
'int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possible.  A floating point\nargument will be truncated towards zero (this does not include a string\nrepresentation of a floating point number!)  When converting a string, use\nthe optional base.  It is an error to supply a base when converting a\nnon-string.  If base is zero, the proper base is guessed based on the\nstring content.  If the argument is outside the integer range a\nlong object will be returned instead.'
>>> print(tm.N.__doc__)
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
non-string.  If base is zero, the proper base is guessed based on the
string content.  If the argument is outside the integer range a
long object will be returned instead.
>>> type(tm.N)
<type 'int'>
>>tm.N.\uu文件__
'int(x[,base])->integer\n\n如果可能,将字符串或数字转换为整数。浮点值\n将被截断为零(这不包括字符串\n浮点数的表示!)转换字符串时,请使用\n可选的基。转换非字符串时\n提供基是错误的。如果基数为零,则根据字符串内容猜测正确的基数。如果参数超出整数范围,\n将返回一个长对象
>>>打印(商标编号、文件)
int(x[,基])->整数
如果可能,将字符串或数字转换为整数。浮点数
参数将被截断为零(这不包括字符串
表示浮点数!)转换字符串时,请使用
可选的基础。转换数据时提供基是错误的
非字符串。如果基数为零,则根据
字符串内容。如果参数超出整数范围a
将返回long对象。
>>>类型(tm.N)

它生成长度为1000的时间序列

>>> df.head()
Out[7]: 
                   A         B         C         D
2000-01-03 -0.734093 -0.843961 -0.879394  0.415565
2000-01-04  0.028562 -1.098165  1.292156  0.512677
2000-01-05  1.135995 -0.864060  1.297646 -0.166932
2000-01-06 -0.738651  0.426662  0.505882 -0.124671
2000-01-07 -1.242401  0.225207  0.053541 -0.234740
>>> len(df)
Out[8]: 1000

N
testing.py
模块中的一个全局变量,用于在模块中测试阵列和其他内容。其默认值为30。例如

np.arange(N * K).reshape((N, K))
Series(randn(N), index=index)
在您发布的代码中,它的使用率很低,因为
makeTimeDataFrame
可以使用
nper
参数进行馈送,如果未提供
nper
,该参数最终会被
N
替代。这是正确的用法,不会让您感到困惑:

df = tm.makeTimeDataFrame(nper=1000)

在模块pandas.util中的pandas中,测试N属性意味着时间序列 请参阅本节中的参考资料:

We could alternatively have used the unit testing function to create a TimeSeries of length 20:


>>>> pandas.util.testing.N = 20
>>>> ts = pandas.util.testing.makeTimeSeries()

.N提供数组类型中的元素数。例如,如果使用颜色贴图,
plt.get_cmap('Pastel1')。N
将返回
9
,因为它由9种颜色组成
plt.get\u cmap('nipy\u spectrum')。N
将返回
256

显然
tm
是一个具有属性的对象
N
。感谢大家的解释。很多人花时间回答了您的问题,请标记一个作为答案。