Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么从一个系列中选择一个特定的值只有在它被迭代时才起作用?_Python_Pandas_Dataframe_Iterator - Fatal编程技术网

Python 为什么从一个系列中选择一个特定的值只有在它被迭代时才起作用?

Python 为什么从一个系列中选择一个特定的值只有在它被迭代时才起作用?,python,pandas,dataframe,iterator,Python,Pandas,Dataframe,Iterator,背景: 我想写一个程序,保存数据帧中单个系列或系列列表中的信息。这涉及在循环中处理单个系列或一组系列。信息保存在dict中。字典和计算特征的方法包含在父类中。有两个子类。第一个子类涉及单个系列,第二个子类涉及系列组(集群) 代码(简称): 一个父类,包含dict的特征和填充方法。 在几个函数中,它有一个函数可以检测一个序列(它的日期值)是否为桥接日 描述单个系列的子类: 表示系列分组的子类: 现在,让我们假设我有以下几点: 单个系列: 一组系列: 获取特定日期函数: def get_speci

背景:

我想写一个程序,保存数据帧中单个系列或系列列表中的信息。这涉及在循环中处理单个系列或一组系列。信息保存在dict中。字典和计算特征的方法包含在父类中。有两个子类。第一个子类涉及单个系列,第二个子类涉及系列组(集群)

代码(简称):

  • 一个父类,包含dict的特征和填充方法。 在几个函数中,它有一个函数可以检测一个序列(它的日期值)是否为桥接日
  • 描述单个系列的子类:
  • 表示系列分组的子类:

  • 现在,让我们假设我有以下几点:

  • 单个系列:
  • 一组系列:
  • 获取特定日期函数:

    def get_specific_dates(data, datetime_arr):
        return data[data.dates.isin(datetime_arr)]
    
    问题:

    当我运行:
    dp=DailyProfile(day)
    时,我得到以下错误:
    序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
    但是,当我运行
    cls=Cluster(days)
    时,没有错误

    进一步调查显示:

    day = get_specific_dates(DATA, ["2019-02-11"])
    days = get_specific_dates(DATA, ["2019-02-11", "2019-03-11", "2019-04-11"])
    
    print(day["day_of_week"]) # prints: 1027 2019-02-11 Name: date, dtype: object
    
    for _, d in days.iterrows():
        print(d["day_of_week"]) prints: Monday, Monday, Thursday
    
    问题:

  • 为什么只有在循环中使用时才能从序列中访问特定值?为什么
    d[“date”]
    在处理单个序列(无循环)时返回序列(索引和值),如果循环中使用相同的序列,则返回值
    d[“date”]
    (特定日期)
  • 如何更改代码以保持层次结构不变,并且可以确定
    DailyProfile
    Cluster
    的特征。换句话说,
    detect\u bridge\u day
    仍然在父类中,并且可以由
    Cluster
    DailyProfile
    使用,以便计算单个系列或群集的特征
  • class Cluster(Characterizable):
        
        def __init__(self, cluster):
            Characterizable.__init__(self)
            self.cluster_data = cluster_data
            self.cluster_size = len(cluster_data)
            self.determine_cluster_characteristics()
    
        def determine_cluster_characteristics(self):
           
            bridge_counter = 0
            for _, tgl in self.cluster_data.iterrows():
                if self.detect_bridge_day(tgl): bridge_counter+=1
                    
            self.characteristics["IsBridgeDay"] = bridge_counter / self.cluster_size
    
    day = get_specific_dates(DATA, ["2019-02-11"]) # function that retrieves series from a df that have a specific date
    
    cluster = get_specific_dates(DATA, ["2019-02-11", "2019-03-11", "2019-04-11"])
    
    def get_specific_dates(data, datetime_arr):
        return data[data.dates.isin(datetime_arr)]
    
    day = get_specific_dates(DATA, ["2019-02-11"])
    days = get_specific_dates(DATA, ["2019-02-11", "2019-03-11", "2019-04-11"])
    
    print(day["day_of_week"]) # prints: 1027 2019-02-11 Name: date, dtype: object
    
    for _, d in days.iterrows():
        print(d["day_of_week"]) prints: Monday, Monday, Thursday