简体   繁体   中英

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.

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? and with R zoo?

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) . 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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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