# How can I split the Dataset using timestamp feature

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-split-the-dataset-using-timestamp-feature%2F43464%3E%5C
**Category:** 🤗Datasets\
**Created:** [June 16, 2023, 6:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-split-the-dataset-using-timestamp-feature%2F43464 "2023-06-16T06:41:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sociopath00](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sociopath00/32/16887_2.png) [@sociopath00](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsociopath00)%5C
**Post date:** [June 16, 2023, 6:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-split-the-dataset-using-timestamp-feature%2F43464%2F1 "2023-06-16T06:41:22Z")

</div>

Hello,

I have timeseries data in csv file which I am loading using following code.

```auto
from datasets import load_dataset

dataset = load_dataset("csv", data_files="mobile_4hr.csv")

```

I know for other dataset we can randomly split like `ds.train_test_split(test_size=0.3)`

But for timeseries data how can I split into train and test using specific date?

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmariosasko)%5C
**Post date:** [June 16, 2023, 4:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-split-the-dataset-using-timestamp-feature%2F43464%2F2 "2023-06-16T16:27:14Z")

</div>

I see two solutions:

- cast the date column to `Value("date64")`, sort the column and split the dataset

```python
from datasets import load_dataset, Value
from datetime import datetime
ds = load_dataset(...)
features = ds.features
features["date"] = Value("date64")
ds = ds.map(lambda ex: {"date": datetime.strptime(ex["date"], date_format)}, features=features)
ds = ds.sort(date)
train_size = 0.7
train_ds = ds.select(range(int(train_size * len(ds))))
test_ds = ds.select(range(int(train_size * len(ds)), len(ds)))

```

- iterate over the dataset rows and store their indices, `train_ds_idx` or `test_ds_idx`, based on the value of `date` and then run `train_ds = ds.select(train_ds_idx)` and `test_ds = ds.select(test_ds_idx)` to build the splits
