> For the complete documentation index, see [llms.txt](https://skywateryang.gitbook.io/timeseriesanalysis101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://skywateryang.gitbook.io/timeseriesanalysis101/2.-zhun-bei-he-chu-li-shi-jian-xu-lie-shu-ju/2.4-qing-xi-shu-ju/2.4.2-gai-bian-shu-ju-ji-shi-jian-pin-lv.md).

# 2.4.2 改变数据集时间频率

通常我们会发现来自不同数据源的时间轴常常无法一一对应，此时就要用到改变时间频率的方法进行数据清洗。由于无法改变实际测量数据的频率，我们能做的是改变数据收集的频率，也就是本节提到的上采样（upsamping）和下采样（downsampling）。

**下采样**

下采样指的是减少数据收集的频率，也就是从原始数据中抽取子集的方式。

以下是一些下采样会用到的场景：

* 数据原始的分辨率不合理：例如有一个记录室外温度的数据，时间频率是每秒钟一次。我们都知道，气温不会在秒钟这个级别有明显的变化，而且秒级的气温数据的测量误差甚至会比数据本身的波动还要大，因此这个数据集有着大量的冗余。在这个案例中，每隔n个元素取一次数据可能更合理一些。
* 关注某个特定季节的信息：如果担心某些数据存在季节性的波动，我们可以只选择某一个季节（或月份）进行分析，例如只选择每年一月份的数据进行分析。
* 进行数据匹配：例如你有两个时间序列数据集，一个更低频（年度数据），一个更高频（月度数据），为了将两个数据集匹配进行下一步的分析，可以对高频数据进行合并操作，如计算年度均值或中位数，从而获得相同时间轴的数据集。

**上采样**

上采样在某种程度上是凭空获得更高频率数据的方式，我们要记住的是使用上采样，只是让我们获得了更多的数据标签，而没有增加额外的信息。

以下是一些上采样会用到的场景：

* 不规律的时间序列：用于处理多表关联中存在不规则时间轴的问题。

  例如现在有两个数据，一个记录了捐赠的时间和数量

  |  amt |     dt    |
  | :--: | :-------: |
  |  99  | 2019-2-27 |
  |  100 |  2019-3-2 |
  |   5  | 2019-6-13 |
  |  15  |  2019-8-1 |
  |  11  | 2019-8-31 |
  | 1200 | 2019-9-15 |

  另一个数据记录了公共活动的时间和代号

  | identifier |    dt    |
  | :--------: | :------: |
  |    q4q42   | 2019-1-1 |
  |   4299hj   | 2019-4-1 |
  |    bbg2    | 2019-7-1 |

  这时我们需要合并这两个表的数据，为每次捐赠打上标签，记录每次捐赠之前最近发生的一次公共活动，这种操作叫做rolling join，关联后的数据结果如下。

  | identifier |  dt  |    amt    |
  | :--------: | :--: | :-------: |
  |    q4q42   |  99  | 2019-2-27 |
  |    q4q42   |  100 |  2019-3-2 |
  |   4299hj   |   5  | 2019-6-13 |
  |    bbg2    |  15  |  2019-8-1 |
  |    bbg2    |  11  | 2019-8-31 |
  |    bbg2    | 1200 | 2019-9-15 |
* 进行数据匹配：类似下采样的场景，例如我们有一个月度的失业率数据，为了和其他数据匹配需要转换成日度的数据，如果我们假定新工作一般都是从每个月第一天开始的，那么可以推演认为这个月每天的失业率都等于该月的失业率。

通过以上的案例我们发现，即使是在十分干净的数据集中，由于需要比较来自不同维度的具有不同尺度的数据，也经常需要使用到上采样和下采样的方法。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://skywateryang.gitbook.io/timeseriesanalysis101/2.-zhun-bei-he-chu-li-shi-jian-xu-lie-shu-ju/2.4-qing-xi-shu-ju/2.4.2-gai-bian-shu-ju-ji-shi-jian-pin-lv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
