是否有可能使用pandas resample
方法,然后只使用第一个(或任何特定的)分区进行应用(或在这方面的任何其他方法)?
举例说明:
def do something(x):
return [x]
my_timeseries.resample("d").apply(do_something)[0]
现在这给了我想要的输出,但也计算了所有其他分区(显然)但是如果我只想从重新采样对象中选择第一个分区,它将自动取其均值并发出警告:
my_timeseries.resample("d")[0].apply(do_something)
>> FutureWarning:
.resample() is now a deferred operation
You called __getitem__(...) on this deferred object which materialized it into a series
by implicitly taking the mean. Use .resample(...).mean() instead
if __name__ == '__main__':
答案 0 :(得分:1)
在以前的熊猫版本中,方法[INFO] com.postfenix:api:war:0.0.1
[INFO] +- com.postfenix:domain:jar:0.0.1:compile
[INFO] | +- com.postfenix:common:jar:0.0.1:compile
[INFO] | | +- org.springframework.social:spring-social-security:jar:1.1.5.RELEASE:compile
[INFO] | | | +- org.springframework.social:spring-social-core:jar:1.1.5.RELEASE:compile
[INFO] | | | \- org.springframework.social:spring-social-web:jar:1.1.5.RELEASE:compile
[INFO] | | +- org.telegram:telegrambots-spring-boot-starter:jar:3.6.1:compile
[INFO] | | | \- org.telegram:telegrambots:jar:3.6.1:compile
[INFO] | | | +- org.telegram:telegrambots-meta:jar:3.6.1:compile
[INFO] | | | | \- com.google.inject:guice:jar:4.1.0:compile
[INFO] | | | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | | | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.9.6:compile
[INFO] | | | | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.9.6:compile
[INFO] | | | | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.9.6:compile
[INFO] | | | +- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.26:compile
[INFO] | | | | +- org.glassfish.jersey.core:jersey-common:jar:2.26:compile
[INFO] | | | | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] | | | | \- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.26:compile
[INFO] | | | +- org.glassfish.jersey.containers:jersey-container-grizzly2-http:jar:2.25.1:compile
[INFO] | | | | +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b32:compile
[INFO] | | | | +- org.glassfish.grizzly:grizzly-http-server:jar:2.3.28:compile
[INFO] | | | | | \- org.glassfish.grizzly:grizzly-http:jar:2.3.28:compile
[INFO] | | | | | \- org.glassfish.grizzly:grizzly-framework:jar:2.3.28:compile
[INFO] | | | | \- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO] | | | +- org.glassfish.jersey.core:jersey-server:jar:2.26:compile
[INFO] | | | | +- org.glassfish.jersey.core:jersey-client:jar:2.26:compile
[INFO] | | | | \- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.26:compile
[INFO] | | | \- org.json:json:jar:20160810:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.3.RELEASE:compile
[INFO] | | | +- org.mongodb:mongodb-driver:jar:3.6.4:compile
[INFO] | | | | +- org.mongodb:bson:jar:3.6.4:compile
[INFO] | | | | \- org.mongodb:mongodb-driver-core:jar:3.6.4:compile
[INFO] | | | \- org.springframework.data:spring-data-mongodb:jar:2.0.8.RELEASE:compile
[INFO] | | | \- org.springframework.data:spring-data-commons:jar:2.0.8.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-validation:jar:2.0.3.RELEASE:compile
[INFO] | | +- eu.bitwalker:UserAgentUtils:jar:1.20:compile
[INFO] | | \- com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:jar:20160924.1:compile
[INFO] | +- org.springframework:spring-context-support:jar:5.0.7.RELEASE:compile
[INFO] | +- org.togglz:togglz-spring-boot-starter:jar:2.5.0.Final:compile
[INFO] | | +- org.togglz:togglz-core:jar:2.5.0.Final:compile
[INFO] | | +- org.togglz:togglz-spring-core:jar:2.5.0.Final:compile
[INFO] | | \- org.togglz:togglz-spring-web:jar:2.5.0.Final:compile
[INFO] | +- org.togglz:togglz-spring-security:jar:2.5.0.Final:compile
[INFO] | +- org.togglz:togglz-mongodb:jar:2.5.0.Final:compile
[INFO] | | \- org.mongodb:mongo-java-driver:jar:3.6.4:compile
[INFO] | +- org.springframework.kafka:spring-kafka:jar:2.1.7.RELEASE:compile
[INFO] | | +- org.springframework:spring-messaging:jar:5.0.7.RELEASE:compile
[INFO] | | +- org.springframework.retry:spring-retry:jar:1.2.2.RELEASE:compile
[INFO] | | \- org.apache.kafka:kafka-clients:jar:1.1.0:compile
[INFO] | | +- org.lz4:lz4-java:jar:1.4:compile
[INFO] | | \- org.xerial.snappy:snappy-java:jar:1.1.7.1:compile
[INFO] | +- org.quartz-scheduler:quartz:jar:2.3.0:compile
[INFO] | | \- com.mchange:mchange-commons-java:jar:0.2.11:compile
[INFO] | +- com.cronutils:cron-utils:jar:7.0.2:compile
[INFO] | +- com.novemberain:quartz-mongodb:jar:2.0.0:compile
[INFO] | | \- org.clojure:clojure:jar:1.7.0:runtime
[INFO] | \- org.apache.tika:tika-core:jar:1.18:compile
[INFO] +- com.postfenix:api-dto:jar:0.0.1:compile
[INFO] +- com.postfenix:api-client:jar:0.0.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.3.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.3.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.6:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.6:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.6:compile
[INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.10.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] | | \- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | +- org.springframework:spring-web:jar:5.0.7.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:2.0.3.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:5.0.6.RELEASE:compile
[INFO] | | \- org.springframework.security:spring-security-core:jar:5.0.6.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-web:jar:5.0.6.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.0.3.RELEASE:compile
[INFO] | +- org.thymeleaf:thymeleaf-spring5:jar:3.0.9.RELEASE:compile
[INFO] | | \- org.thymeleaf:thymeleaf:jar:3.0.9.RELEASE:compile
[INFO] | | +- org.attoparser:attoparser:jar:2.0.4.RELEASE:compile
[INFO] | | \- org.unbescape:unbescape:jar:1.1.5.RELEASE:compile
[INFO] | \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.3.RELEASE:compile
[INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.31:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.31:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.31:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.0.3.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.0.3.RELEASE:compile
[INFO] | | \- org.springframework.boot:spring-boot-actuator:jar:2.0.3.RELEASE:compile
[INFO] | \- io.micrometer:micrometer-core:jar:1.0.5:compile
[INFO] | +- org.hdrhistogram:HdrHistogram:jar:2.1.10:compile
[INFO] | \- org.latencyutils:LatencyUtils:jar:2.0.3:compile
[INFO] +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | +- net.minidev:json-smart:jar:2.3:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- com.jayway.jsonpath:json-path-assert:jar:2.4.0:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | \- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- javax:javaee-web-api:jar:7.0:provided
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] | \- commons-codec:commons-codec:jar:1.11:compile
[INFO] +- io.springfox:springfox-swagger2:jar:2.6.0:compile
[INFO] | +- io.swagger:swagger-annotations:jar:1.5.10:compile
[INFO] | +- io.swagger:swagger-models:jar:1.5.10:compile
[INFO] | +- io.springfox:springfox-spi:jar:2.6.0:compile
[INFO] | | \- io.springfox:springfox-core:jar:2.6.0:compile
[INFO] | +- io.springfox:springfox-schema:jar:2.6.0:compile
[INFO] | +- io.springfox:springfox-swagger-common:jar:2.6.0:compile
[INFO] | +- io.springfox:springfox-spring-web:jar:2.6.0:compile
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] | +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] | +- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
[INFO] | \- org.mapstruct:mapstruct:jar:1.0.0.Final:compile
[INFO] +- io.springfox:springfox-swagger-ui:jar:2.6.0:compile
[INFO] +- org.togglz:togglz-console:jar:2.5.0.Final:compile
[INFO] | \- org.togglz:togglz-servlet:jar:2.5.0.Final:compile
[INFO] +- com.jayway.restassured:rest-assured:jar:2.9.0:test
[INFO] | +- org.codehaus.groovy:groovy:jar:2.4.15:test
[INFO] | +- org.codehaus.groovy:groovy-xml:jar:2.4.15:test
[INFO] | +- org.apache.httpcomponents:httpmime:jar:4.5.5:compile
[INFO] | +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2.1:test
[INFO] | +- com.jayway.restassured:json-path:jar:2.9.0:test
[INFO] | | +- org.codehaus.groovy:groovy-json:jar:2.4.15:test
[INFO] | | \- com.jayway.restassured:rest-assured-common:jar:2.9.0:test
[INFO] | \- com.jayway.restassured:xml-path:jar:2.9.0:test
[INFO] +- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.3.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
[INFO] +- org.springframework:spring-core:jar:5.0.7.RELEASE:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-aspects:jar:5.0.7.RELEASE:compile
[INFO] | \- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] +- org.springframework:spring-context:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-expression:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] +- org.springframework.boot:spring-boot:jar:2.0.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.3.RELEASE:compile
[INFO] | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.0.3.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.0.3.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.3.RELEASE:test
[INFO] | +- org.assertj:assertj-core:jar:3.9.1:test
[INFO] | +- org.mockito:mockito-core:jar:2.15.0:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.7.11:test
[INFO] | | \- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-test:jar:5.0.7.RELEASE:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.5.1:test
[INFO] +- javax.enterprise:cdi-api:jar:2.0-EDR1:compile
[INFO] | +- javax.el:javax.el-api:jar:3.0.0:compile
[INFO] | +- javax.interceptor:javax.interceptor-api:jar:1.2:compile
[INFO] | \- javax.inject:javax.inject:jar:1:compile
[INFO] +- javax.transaction:jta:jar:1.1:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.7:compile
[INFO] +- commons-validator:commons-validator:jar:1.6:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] | +- commons-digester:commons-digester:jar:1.8.1:compile
[INFO] | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- commons-io:commons-io:jar:2.6:compile
[INFO] +- org.apache.commons:commons-compress:jar:1.16.1:compile
[INFO] | \- org.objenesis:objenesis:jar:2.6:compile
[INFO] +- org.apache.commons:commons-text:jar:1.3:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] \- org.awaitility:awaitility:jar:3.1.0:test
[INFO]
使用参数DataFrame.resample
进行下采样或重新采样,其中how
为下采样的默认值(请查看版本0.17的文档) here)
现在,该方法返回一个重采样器对象,必须对其进行显式操作才能进行下采样/上采样。这就是引发FutureWarning并在对数据建立索引之前隐式调用mean
的原因。
使用mean
时,您使用my_timeseries.resample("d").apply(do_something)[0]
对数据进行重新采样,然后访问元素do_something
。
另一方面,不可能直接索引0
,并且出于兼容性的考虑,大熊猫在索引之前会调用my_timeseries.resample("d")
。最好的办法就是使用您所说的mean
。
答案 1 :(得分:0)
所以经过一番摆弄之后,如果还有其他人感兴趣的话,我得到了一个不太好的但可行的解决方案:
resampler = my_timeseries.resample("d")
my_timeseries[resampler.indices[list(resampler.indices)[0]]].apply(do_something)
通过这种方式,我们可以获得重采样器对象包含的“分组”项目的索引,并可以利用这些索引在原始Series
中进行选择。
注意:这可能仅适用于python 3.6+(我尚未在较低版本上进行过实际测试),因为它要求对索引dict进行排序(从3.6开始) ,它只是一个实现细节,但应该是从python 3.7开始的有保证的规范。