xarray-按特定日期范围对数据进行分组

时间:2019-08-19 04:37:17

标签: python pandas python-xarray

我有一个名为 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.cardview.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="12"> <androidx.appcompat.widget.AppCompatCheckBox android:id="@+id/checkid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:padding="4dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="9" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textViewTitleid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/imageView" android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)" android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" android:textColor="#000000" /> <TextView android:id="@+id/textViewShortDescid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textViewTitleid" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@id/imageView" android:text="13.3 Inch, 256 GB" android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" /> </LinearLayout> <!--<TextView--> <!--android:id="@+id/textViewRatingid"--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"--> <!--android:layout_below="@id/textViewShortDescid"--> <!--android:layout_marginLeft="5dp"--> <!--android:layout_marginTop="5dp"--> <!--android:layout_toRightOf="@id/imageView"--> <!--android:background="@color/colorPrimary"--> <!--android:paddingLeft="15dp"--> <!--android:paddingRight="15dp"--> <!--android:text="4.7"--> <!--android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"--> <!--android:textStyle="bold" />--> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:orientation="horizontal"> <TextView android:id="@+id/textViewPriceid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="INR 56990" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" > <Button android:id="@+id/minusbtn" android:layout_width="35dp" android:layout_height="35dp" android:background="@null" android:text="-" android:textSize="16dp" /> <TextView android:id="@+id/counter_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#DFDDDD" android:padding="2dp" android:text="0" /> <Button android:id="@+id/plusbtn" android:layout_width="35dp" android:layout_height="35dp" android:background="@null" android:text="+" android:textSize="16dp" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </androidx.recyclerview.widget.RecyclerView> </androidx.constraintlayout.widget.ConstraintLayout> package com.example.mycart; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.widget.AppCompatCheckBox; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> { private Context context; private List<Product> productList; //static int count =0; public ProductAdapter(Context context, List<Product> productList) { this.context = context; this.productList = productList; } @NonNull @Override public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.list, null); return new ProductViewHolder(view); } @Override public void onBindViewHolder(@NonNull final ProductViewHolder holder, int position) { final Product product = productList.get(position); holder.textViewTitle.setText(product.getTitle()); holder.textViewDesc.setText(product.getShortdesc()); // holder.textViewRating.setText(String.valueOf(product.getRating())); holder.textViewPrice.setText(String.valueOf(product.getPrice())); holder.imageView.setImageDrawable(context.getResources().getDrawable(product.getImage())); //holder.counterText.setText(String.valueOf(getItemCount())); holder.count.setText(String.valueOf(product.getCount())); holder.plusbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { product.setCount(product.getCount() + 1); holder.count.setText(String.valueOf(product.getCount())); //product.setCount(product.getCount() + 1); //holder.count.setText(String.valueOf(product.getCount()+1)); // product.setCount(product.getCount() + 1); //holder.count.setText(product.getCount()); } }); holder.minusbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (product.getCount() > 1) { product.setCount(product.getCount() - 1); holder.count.setText(String.valueOf(product.getCount())); //product.setCount(product.getCount() - 1); //holder.count.setText(String.valueOf(product.getCount()-1)); //product.setCount(product.getCount() - 1); //holder.count.setText(product.getCount()); } } }); } @Override public int getItemCount() { return productList.size(); } public class ProductViewHolder extends RecyclerView.ViewHolder { ImageView imageView; TextView textViewTitle, textViewDesc, textViewPrice, count; AppCompatCheckBox checkBox; Button minusbtn, plusbtn; public ProductViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.imageView); textViewTitle = itemView.findViewById(R.id.textViewTitleid); textViewDesc = itemView.findViewById(R.id.textViewShortDescid); // textViewRating =itemView.findViewById(R.id.textViewRatingid); textViewPrice = itemView.findViewById(R.id.textViewPriceid); count = itemView.findViewById(R.id.counter_text); checkBox = itemView.findViewById(R.id.checkid); minusbtn = itemView.findViewById(R.id.minusbtn); plusbtn = itemView.findViewById(R.id.plusbtn); } } } package com.example.mycart; public class Product { private int id; private String title, shortdesc; private double price; private int image; private int count = 1; public Product(int id, String title, String shortdesc, double price, int image) { this.id = id; this.title = title; this.shortdesc = shortdesc; this.price = price; this.image = image; } public void setCount(int count) { this.count = count; } public int getCount() { return count; } public int getId() { return id; } public String getTitle() { return title; } public String getShortdesc() { return shortdesc; } public double getPrice() { return price; } public int getImage() { return image; } } package com.example.mycart; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; ProductAdapter adapter; List<Product> productList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); productList = new ArrayList<>(); recyclerView =(RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); productList.add( new Product( 1, "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra", "13.3 inch, Silver, 1.35 kg", 6500.00,R.drawable.macbook)); productList.add( new Product( 1, "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra", "13.3 inch, Silver, 1.35 kg", 6500.00,R.drawable.macbook)); productList.add( new Product( 1, "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra", "13.3 inch, Silver, 1.35 kg", 6500.00,R.drawable.macbook)); adapter = new ProductAdapter(this,productList); recyclerView.setAdapter(adapter); } } 的xarray DataArray对象。

da_ffdi_daily_max

<xarray.DataArray 'FFDI' (time: 3653, latitude: 106, longitude: 193)> dask.array<shape=(3653, 106, 193), dtype=float32, chunksize=(1, 106, 193)> Coordinates: * time (time) datetime64[ns] 2008-01-01 2008-01-02 ... 2017-12-31 * latitude (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9 * longitude (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0 坐标的范围是2008年1月1日至2017年12月31日。我想将11月的FFDI数据分为三类,即timeNov 01 - 10Nov 11-20

如何通过Nov 21-30的{​​{1}}函数来实现?

1 个答案:

答案 0 :(得分:1)

Groupby通常会尝试将所有数据分类为特定的组,而您似乎正在尝试提取数据的子集。您可以使用常规的日期时间索引制作所需的子集。

a = xr.tutorial.open_dataset('air_temperature')

nov_1_10 = a['air'][(a['time'].dt.day <= 10) & (a['time'].dt.month == 11)]
nov_11_20 = a['air'][(a['time'].dt.day <= 20) & (a['time'].dt.day >= 11) & (a['time'].dt.month == 11)]
nov_21_30 = a['air'][(a['time'].dt.day >= 21) & (a['time'].dt.month == 11)]