简体   繁体   English

熊猫时间序列和R动物园

[英]pandas timeseries and R zoo

I get really stuck with this silly task : 我真的被这个愚蠢的任务困住了:

I have two Pandas time series. 我有两个熊猫时间序列。 Some time indexes may be different between them. 它们之间的某些时间索引可能有所不同。 I want, for the both series to get rid of the lines containing different time stamps, and keep, for both, only the lines with common time stamps. 我想让这两个系列摆脱包含不同时间戳的行,而只保留具有共同时间戳的行。

I have the same issue with two zoo series in R. 我对R中的两个动物园系列有同样的问题。

To give an example, here are my series : 举个例子,这是我的系列:

a = 2015-05-01 15.2
    2015-05-02 16.3
    2015-05-03 17.4
    2015-05-04 18.5
    2015-05-05 19.6

b = 2015-05-04 'a'
    2015-05-05 'b'
    2015-05-06 'c'

And then result I want to reach is : 然后我想要达到的结果是:

a = 2015-05-04 18.5
    2015-05-05 19.6        

b = 2015-05-04 'a'
    2015-05-05 'b'

So that I keep only rows for which the time index is in the intersection of the time indices of the old series. 因此,我只保留时间索引在旧系列的时间索引的交点中的行。 How can that be Done with Pandas.Series? 如何用Pandas.Series完成? and with R zoo? 和R动物园?

Please excuse me if the question appears to be silly. 如果这个问题看起来很愚蠢,请原谅。 I really searched a lot without success. 我确实搜索了很多但都没有成功。 By the way, what is this operation called? 顺便说一句,这个操作叫什么? Merging? 合并? Aligning? 对齐?

Thanks a lot for help. 非常感谢您的帮助。

在熊猫中,有一个align操作,它将像这样工作:

a, b = a.align(b, join='inner')

In R with zoo it would be: merge(a, b, all = FALSE, retclass = NULL) . 在带动物园的R中,它将是: merge(a, b, all = FALSE, retclass = NULL) Note that (1) all=FALSE returns the intersection only and (2) retclass=NULL returns the merged series by writing the outputs back to the arguments. 请注意,(1) all=FALSE仅返回交集,而(2) retclass=NULL通过将输出写回到参数来返回合并的序列。

Below is a self contained reproducible example. 以下是一个自包含的可复制示例。

# define inputs and read them into R as zoo objects

Lines.a = "2015-05-01 15.2
    2015-05-02 16.3
    2015-05-03 17.4
    2015-05-04 18.5
    2015-05-05 19.6"

Lines.b <- "2015-05-04 a
    2015-05-05 b
    2015-05-06 c"

library(zoo)
a <- read.zoo(text = Lines.a)
b <- read.zoo(text = Lines.b)

# merge the inputs and show result

merge(a, b, all = FALSE, retclass = NULL)

a
## 2015-05-04 2015-05-05 
      18.5       19.6 
b
## 2015-05-04 2015-05-05 
     a          b 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM