简体   繁体   English

从另一个定义函数获取变量以在python3中的另一个定义函数中读取时发生名称错误

[英]Name Error when getting variable from another define function to be read in another define function in python3

I'm not sure how to solve the problem I have encountered, how do I solve this? 我不确定如何解决遇到的问题,该如何解决?

My python code I'm using: 我正在使用的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)

My Error: 我的错误:

NameError Traceback (most recent call last) in () 10 #print(type(dtStart)) <-- check if dtStart has been converted to datetime type 11 ---> 12 selTimestamprange(dtStart, dtEnd, Data) NameError Traceback((last last call last)in()10 #print(type(dtStart))<-检查dtStart是否已转换为日期时间类型11 ---> 12 selTimestamprange(dtStart,dtEnd,Data)

NameError: name 'Data' is not defined NameError:名称“数据”未定义

The Data is local variable which is only available in function readcsv(x) , you could return it in the function and assign the result to variable Data , then you can use the variable Data . Data是局部变量,仅在函数readcsv(x)可用,您可以在函数readcsv(x)其返回并将结果分配给变量Data ,然后可以使用变量Data Like this: 像这样:

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])
    return Data

Data = readcsv('MainD.csv')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个文件 python 中定义在 function 中使用的变量 - define a variable used in a function in another file python 有什么方法可以从 python3 中的字符串定义 function 名称? - Is there any method to define a function name from string in python3? 如何定义 function 以基于另一个变量创建具有特定名称的变量? (Python 3) - How can I define a function to create a variable with an especific name based on another variable? (Python 3) 如何从另一个多变量函数定义一个变量函数 - How to define a one variable function from another multivariable function 如何调用一个函数,它在python中的另一个函数中定义? - How to call a function, it define in another function in python? 当我使用它来定义另一个 function 时,来自预定义 function 的 RaiseError 被调用 - RaiseError from predefined function is getting called when I use it to define another function 如果仅在定义函数中设置变量,如何从另一个Python脚本导入变量? - How do I import a variable from another Python script if the variable is only set within a define function? 无法将变量从一个 function 定义到另一个 - Cannot define variable from one function into another one 在python中定义函数时锁定外部变量 - lock external variable when define function in python 如何在 Python 中的另一个自定义 function 中使用自定义 function 的局部变量 - How can I use the local variable of a self-define function in another self-def function in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM