【问题标题】:numpy.genfromtxt , are uneven spaces between columns causing dtype errors?numpy.genfromtxt ,列之间的不均匀空间是否会导致 dtype 错误?
【发布时间】:2020-04-02 23:49:14
【问题描述】:

我正在使用的数据可以在这个gist找到,

看起来像:

07-11-2018 18:34:35 -2.001   5571.036 -1.987
07-11-2018 18:34:50 -1.999   5570.916 -1.988

image of code and output in Jupyter Notebook

打电话时

TB_CAL_array = np.genfromtxt('calbath_data/TB118192.TXT',
                            skip_header = 10,
                            dtype = ([("date", "<U10"), ("time","<U8"), ("bathtemp", "<f8"), 
                                    ("SBEfreq", "<f8"), ("SBEtemp", "<f8")])

                               )

数组的输出是:

array([('07-11-2018', '18:34:35', -2.001e+00, 5571.036, -1.987),
   ('07-11-2018', '18:34:50', -1.999e+00, 5570.916, -1.988),

数据作为元组的结构化 ndarray 输出,并且是非同质数组,因为它包含字符串和浮点数。 numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why?

注意:数据输出的第三列已被视为指定的 dtype 以外的内容。

输出应该是-2.001,但它是-2.001e+00

注意:请注意,第五列具有相同的输入格式和 dtype 指定,但是在 genfromtxt 函数期间没有发生数据转换...

我能发现“bathtemp”和“SBEtemp”之间的唯一区别是“bathtemp”列之后有两个额外的空格......

但是基于 numpy.genfromtxt IO documentation 这应该没关系,因为连续的空格应该自动被视为分隔符。:

delimiter : str, int, or sequence, 可选 用于分隔值的字符串。默认情况下,任何连续的空格都充当分隔符。也可以提供整数或整数序列作为每个字段的宽度。

“bathtemp”列后的多余空格是否会导致错误?如果是这样,我该如何解决?

【问题讨论】:

  • 第 3 列是有效的浮点数 - 科学记数法。
  • 我没有看到任何错误。你得到了一个与 dtype 匹配的数组。
  • stackoverflow.com/questions/59275231/… - 关于 pandas 和 numpy 科学记数法控件

标签: arrays numpy python-import delimiter genfromtxt


【解决方案1】:

使用您的样品:

In [136]: txt="""07-11-2018 18:34:35 -2.001   5571.036 -1.987 
     ...: 07-11-2018 18:34:50 -1.999   5570.916 -1.988"""                       
In [137]: np.genfromtxt(txt.splitlines(), dtype=None, encoding=None)            
Out[137]: 
array([('07-11-2018', '18:34:35', -2.001, 5571.036, -1.987),
       ('07-11-2018', '18:34:50', -1.999, 5570.916, -1.988)],
      dtype=[('f0', '<U10'), ('f1', '<U8'), ('f2', '<f8'), ('f3', '<f8'), ('f4', '<f8')])

和你的数据类型:

In [139]: np.genfromtxt(txt.splitlines(), dtype= ([("date", "<U10"), ("time","<U
     ...: 8"), ("bathtemp", "<f8"),  
     ...:                                     ("SBEfreq", "<f8"), ("SBEtemp", "<
     ...: f8")]) 
     ...: , encoding=None)                                                      
Out[139]: 
array([('07-11-2018', '18:34:35', -2.001, 5571.036, -1.987),
       ('07-11-2018', '18:34:50', -1.999, 5570.916, -1.988)],
      dtype=[('date', '<U10'), ('time', '<U8'), ('bathtemp', '<f8'), ('SBEfreq', '<f8'), ('SBEtemp', '<f8')])

-2.001e+00 等值与-2.001 相同。 numpy 在值的范围足够大或某些值太小而无法很好显示时选择使用科学计数法。

例如,如果我将其中一个值更改为更小的值:

In [140]: data = _                                                              
In [141]: data['bathtemp']                                                      
Out[141]: array([-2.001, -1.999])
In [142]: data['bathtemp'][1] *= 0.001                                          
In [143]: data['bathtemp']                                                      
Out[143]: array([-2.001e+00, -1.999e-03])

-2.001 不变(显示样式除外)。

我的猜测是某些bathtemp 值(您没有显示)更接近于零。

【讨论】:

  • 谢谢,这个解释很有道理!我在第三列中确实有 0.000 的值。奇怪的是 numpy 会自动使用科学记数法,而 pandas 不会...
【解决方案2】:

由于 skipinitialspace=True 可选输入,我能够通过切换到 pd.read_csv 获得我正在寻找的输出(请参阅此处以获取 reference):

skipinialspace : bool, 默认 False 分隔符后跳过空格。

输入

colnames = ['date', 'time', 'bathtemp', 'SBEfreq', 'SBEtemp']
TB_CAL   = pd.read_csv("calbath_data/TB118192.CAL", header=None, skiprows=10, delimiter=" ", skipinitialspace=True, names=colnames )

输出

    date    time    bathtemp    SBEfreq SBEtemp
0   07-11-2018  18:34:35    -2.001  5571.036    -1.987
1   07-11-2018  18:34:50    -1.999  5570.916    -1.988
2   07-11-2018  18:35:06    -1.997  5571.058    -1.987

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多