Python函数调用之间的CSV文件丢失

时间:2017-06-19 21:59:40

标签: python csv pandas dataframe nonetype

我正在编写一个脚本,我在其中绘制了一个pandas数据框。我首先在特定Python模块的一个文件中预处理数据。然后我从模块中导入该函数。当我在新的Python文件中调用该函数时,它认为数据框是一个None类型的对象,而在原始函数中它正确打印数据框。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import os
from Preprocessing import sample_difference as sd

files = [
    'PickUpPhoneAccelerometer1.csv',
    'PickUpPhoneAccelerometer2.csv',
    'PickUpPhoneAccelerometer3.csv',
    'Wave1Accelerometer.csv',
    'Wave2Accelerometer.csv',
    'Wave3Accelerometer.csv'
]


def segment_energy(data, th):
    print data
    mag = np.array([np.linalg.norm(data['x']), np.linalg.norm(data['y']), np.linalg.norm(data['z'])])

运行此数据时,数据为None

这是另一个Python文件sample_difference

 def sample_difference(filename):
     df = pd.read_csv(filename, header=None, names=['timestamp', 'time skipped', 'x', 'y', 'z', 'label']).set_index('timestamp')
     df.assign(dx=df.x.diff(), dy=df.y.diff(), dz=df.z.diff())
     print df

这会正确打印出数据框。运行最终脚本时的错误是:

line 17, in segment_energy
mag = np.array([np.linalg.norm(data['x']), np.linalg.norm(data['y']), np.linalg.norm(data['z'])])
TypeError: 'NoneType' object has no attribute '__getitem__'

我在同一个文件中调用segment_energy

for f in files:
    with open(os.path.join("/Users", "myname", "PycharmProjects", "sensorLogProject", "Data", f), 'rU') as my_file:
        segment_energy(sd.sample_difference(my_file), 2)

1 个答案:

答案 0 :(得分:2)

首先,您需要使用文件名而不是文件对象调用sample_difference函数:

for f in files:
    filename = os.path.join("/Users", "myname", "PycharmProjects", "sensorLogProject", "Data", f)
    segment_energy(sd.sample_difference(filename), 2)

第二,sample_difference函数应返回数据帧而不是打印:

def sample_difference(filename):
     df = pd.read_csv(filename, header=None, names=['timestamp', 'time skipped', 'x', 'y', 'z', 'label']).set_index('timestamp')
     df.assign(dx=df.x.diff(), dy=df.y.diff(), dz=df.z.diff())
     return df