微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么只有在迭代后才能从系列中选择一个特定的值呢?

如何解决为什么只有在迭代后才能从系列中选择一个特定的值呢?

背景

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

代码(简化版):

  1. 一个父类,包含特征字典和填充它的方法。 在几个函数中,它具有一个检测序列号(日期值)是否为过渡日的函数
class Characterizable:
    
    def __init__(self):
        self.characteristics = {
        "IsMonday" : 0.0,# ...
        }

    def detect_bridge_day(self,dp):
        
        bridge_dates_for_ye = get_special_dates_for_type_and_year("bridge",dp["date"][:4])
        date_without_yr = dp["date"][5:]
        flag = False
        for date in bridge_dates_for_ye:
            if (date_without_yr == date):
                if flag == False:
                    flag = True
        return flag

  1. 描述单个系列的子类:
class DailyProfile(Characterizable):
    
    def __init__(self,dp):
        Characterizable.__init__(self)
        self.dp= dp
        self.determine_profile_characteristics()
        
    def determine_profile_characteristics(self):
        
        if self.detect_bridge_day(self.dp):
            self.set_char_and_its_opposite("IsBridgeDay") # set IsBridgeDay in the characteristics-dict
  1. 代表系列分组的子类:
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

现在,让我说以下内容

  1. 一个系列:
day = get_specific_dates(DATA,["2019-02-11"]) # function that retrieves series from a df that have a specific date
  1. 一组系列:
cluster = get_specific_dates(DATA,["2019-02-11","2019-03-11","2019-04-11"])

get_specific_dates函数

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

问题:

当我运行:dp = DailyProfile(day)时,出现以下错误The truth value of a Series is ambiguous. Use a.empty,a.bool(),a.item(),a.any() or a.all(). 但是,当我运行cls = Cluster(days)时,没有错误

进一步的调查显示

day = get_specific_dates(DATA,["2019-02-11"])
days = get_specific_dates(DATA,"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

问题:

  1. 为什么仅在循环中使用序列时才能访问序列中的特定值?为什么在处理单个序列(无循环)和d["date"](特定日期)的值时,d["date"]返回一个序列(索引和值),如果在循环中使用相同的序列呢? / li>
  2. 如何更改代码,使层次结构保持不变,并且可以为DailyProfileCluster确定特征。换句话说,detect_bridge_day仍在父类中,ClusterDailyProfile都可以使用library(shiny) msLocation <- "msLoc" searchMWText <- "searchMW" bid <- "2333333" fulltext <- "fulldisplay" ui <- fluidPage( titlePanel("Run server codes conditionally"),sidebarLayout( sidebarPanel( helpText("Evaluate input and run different parts of the code depending on the output functions"),br(),sliderInput("rand","select seed",min = 1,max = 50,step = 1,value = 1) ),mainPanel( fluidRow(conditionalPanel("output.rand == 1"),tags$h4("Here comes the default part"),textoutput("defaultCalc")),fluidRow(conditionalPanel("output.randomint != 1",tags$h4("I can evaluate if the chosen number is even or odd."),textoutput("evenodd") ),fluidRow(conditionalPanel("output.evenodd == 'Number is even'",tags$h4("Number even calculation "),textoutput("msLoc"),textoutput("searchMW"),textoutput("defaultID"),br() ),fluidRow(conditionalPanel("output.evenodd == 'Number is odd'",tags$h4("Here is some id:",textoutput("id")),textoutput("displayFull") ) ) ) ) ))) # server <- function(input,output) { rand1 <- reactive({ if(is.null(input$rand)){return(NULL)} rn <- input$rand return(rn) }) randomint <- reactive({ seedn <- rand1() set.seed(seedn) rint <- sample(1:50,1) return(rint) }) calc1 <- reactive({ intn <- randomint() modn <- intn %% 2 return(modn) }) evenOdd <- reactive({ modn <- calc1() if(modn == 0){valueText = "Number is even"} if(modn != 0){valueText = "Number is odd"} return(valueText) }) idtext <- reactive({ idint <- sample(1:10000,3) idint <- as.character(idint) idint <- paste(idint,collapse = "") return(idint) }) output$defaultCalc <- renderText({ as.character(randomint()) }) output$evenodd <- renderText({ evenOdd() }) output$searchMW <- renderText({ searchMWText }) output$defaultID <- renderText({ bid }) output$id <- renderText({ idtext() }) output$displayFull <- renderText({ fulltext }) } shinyApp(ui = ui,server = server) 来计算单个序列或群集的特征吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?