从另一个定义函数获取变量时,名称出错在python3

时间:2017-08-15 02:38:46

标签: python python-3.x csv

我不确定如何解决我遇到的问题,我该如何解决?

我的python代码我正在使用:

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
import numpy as np
from datetime import date,time,datetime
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt

def readcsv(x): #def function to read csv files based on input below
    Data = pd.read_csv(x, parse_dates=['Time_Stamp'], infer_datetime_format=True)
    Data['Date'] = Data.Time_Stamp.dt.date 
    Data['Time'] = Data.Time_Stamp.dt.time 

    Data['Time_Stamp'] = pd.to_datetime(Data['Time_Stamp']) 
    print(Data[1:6])

readcsv('MainD.csv')

def selTimestamprange(dtStart, dtEnd, Data):
        timestamprange = (Data['Time_Stamp'] > dtStart) & (Data['Time_Stamp'] <= dtEnd)
        TimeRange = Data.loc(timestamprange)
        TimeRange.plot(x='Time_stamp', y='AC_Input_Current', style='-', color='black')

date_string_start = '2017-06-13 16:00:00'
date_string_end = '2017-06-13 16:40:00'
dtStart = datetime.strptime(date_string_start, '%Y-%m-%d %H:%M:%S')
dtEnd = datetime.strptime(date_string_end,'%Y-%m-%d %H:%M:%S')

selTimestamprange(dtStart, dtEnd, Data)

我的错误:

  

NameError Traceback(最近一次调用   最后)in()        10 #print(type(dtStart))&lt; - 检查dtStart是否已转换为datetime类型        11   ---&GT; 12 selTimestamprange(dtStart,dtEnd,Data)

     

NameError:name&#39; Data&#39;未定义

1 个答案:

答案 0 :(得分:0)

Data是局部变量,仅在函数readcsv(x)中可用,您可以在函数中返回它并将结果赋值给变量Data,然后您可以使用变量{ {1}}。像这样:

Data