小编rpe*_*ski的帖子

Pandas累计和列的条件

我没有在其他地方找到答案,所以我需要问一下.可能是因为我不知道如何正确命名它.(英语不是我的原始语言)

我有大的日期时间数据框.时间在这里很重要.df中的一列具有值[Nan,1,-1].我需要执行快速计算,以便在值发生变化时进行累积和重置.

例.

    Time                 sign    desire_value
2014-01-24 05:00:00      Nan     Nan 
2014-01-24 06:00:00      Nan     Nan
2014-01-24 07:00:00      Nan     Nan 
2014-01-24 08:00:00      1       1
2014-01-24 09:00:00      1       2
2014-01-24 10:00:00      1       3
2014-01-24 11:00:00      -1      1
2014-01-24 12:00:00      -1      2
2014-01-24 13:00:00      -1      3
2014-01-24 14:00:00      -1      4
2014-01-24 15:00:00      -1      5
2014-01-24 16:00:00      1       1
2014-01-24 17:00:00      1       2
2014-01-24 18:00:00      1       3
2014-01-24 19:00:00      -1      1
2014-01-24 20:00:00      -1      2  
2014-01-24 21:00:00      1       1
2014-01-24 22:00:00      1       2
Run Code Online (Sandbox Code Playgroud)

我有使用函数的工作解决方案,但效率不高.

    df['sign_1'] …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

6
推荐指数
1
解决办法
5581
查看次数

Rspec 测试只读模型

我在 rails 5 中有一个小应用程序。我尝试使用 Rspec 来测试它,但使用只读模型失败了。

我有只读模型 Product 和普通模型 ProductMsp。第二个 a 是与 Product 关联的所有价格的列表,并存储在 Rails DB 中。产品是与另一个数据库的外部只读连接,用于撤消产品列表。

我将与产品的关联添加到 spec/factories/product_msps.rb。

FactoryGirl.define do
  factory :product_msp do
    product_id 11
    initial_date '01-01-2017'
    expiry_date '31-01-2017'
    msp 9999.99
    association :product
  end
end
Run Code Online (Sandbox Code Playgroud)

我没有添加任何新的测试。当我再次运行 rspec 时,我遇到了失败。可能 rspec 想要创建产品,但由于只读数据库表而无法创建。

Failures:
    1) ProductMsp Validations initial_date cannot by empty
    Failure/Error: pm = FactoryGirl.build(:product_msp, initial_date: nil)    
        ActiveRecord::ReadOnlyRecord:
            Product is marked as readonly
                 # ./spec/models/product_msp_spec.rb:17:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

下面是失败的测试:(还有更多的失败)

require 'rails_helper'

RSpec.describe ProductMsp, type: :model do
    it 'initial_date cannot …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec-rails ruby-on-rails-5

2
推荐指数
1
解决办法
1318
查看次数