- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像这样加载的 DataFrame
minData = pd.read_csv(
currentSymbol["fullpath"],
header = None,
names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Split Factor', 'Earnings', 'Dividends'],
parse_dates = [["Date", "Time"]],
date_parser = lambda x : datetime.datetime.strptime(x, '%Y%m%d %H%M'),
index_col = "Date_Time",
sep=' ')
数据是这样的
>>> minData.index
<class 'pandas.tseries.index.DatetimeIndex'>
[1998-01-02 09:30:00, ..., 2013-12-09 16:00:00]
Length: 1373036, Freq: None, Timezone: None
>>>
>>> minData.head(5)
Open High Low Close Volume \
Date_Time
1998-01-02 09:30:00 8.70630 8.70630 8.70630 8.70630 420.73
1998-01-02 09:35:00 8.82514 8.82514 8.82514 8.82514 420.73
1998-01-02 09:42:00 8.79424 8.79424 8.79424 8.79424 420.73
1998-01-02 09:43:00 8.76572 8.76572 8.76572 8.76572 1262.19
1998-01-02 09:44:00 8.76572 8.76572 8.76572 8.76572 420.73
Split Factor Earnings Dividends Active
Date_Time
1998-01-02 09:30:00 4 0 0 NaN
1998-01-02 09:35:00 4 0 0 NaN
1998-01-02 09:42:00 4 0 0 NaN
1998-01-02 09:43:00 4 0 0 NaN
1998-01-02 09:44:00 4 0 0 NaN
[5 rows x 9 columns]
我可以像这样从我的 DataFrame 中选择行
>>> minData["2004-12-20"]
Open High Low Close Volume \
Date_Time
2004-12-20 09:30:00 35.8574 35.9373 35.8025 35.9273 154112.00
2004-12-20 09:31:00 35.8924 35.9174 35.8824 35.8874 17021.50
2004-12-20 09:32:00 35.8874 35.8924 35.8824 35.8824 17079.50
2004-12-20 09:33:00 35.8874 35.9423 35.8724 35.9373 32491.50
2004-12-20 09:34:00 35.9373 36.0023 35.9174 36.0023 40096.40
2004-12-20 09:35:00 35.9923 36.2071 35.9923 36.1471 67088.90
...
我有这样的日期(从不同的文件读取)
>>> ts
Timestamp('2004-12-20 00:00:00', tz=None)
>>>
我想将这一天所有分钟的“事件”列设置为 True。
我可以用这个来做
minData.loc['2004-12-20',"Active"] = True
我可以用这段疯狂的代码对我的时间戳日期做同样的事情
minData.loc[str(ts.year) + "-" + str(ts.month) + "-" + str(ts.day),"Active"] = True
是的,这就是从 TimeStamp 对象创建一个字符串!
我知道一定有更好的方法来做到这一点..
最佳答案
我真的会这样做
In [20]: df = DataFrame(np.random.randn(10,1),index=date_range('20130101 23:55:00',periods=10,freq='T'))
In [21]: df['Active'] = False
In [22]: df
Out[22]:
0 Active
2013-01-01 23:55:00 0.273194 False
2013-01-01 23:56:00 2.869795 False
2013-01-01 23:57:00 0.980566 False
2013-01-01 23:58:00 0.176711 False
2013-01-01 23:59:00 -0.354976 False
2013-01-02 00:00:00 0.258194 False
2013-01-02 00:01:00 -1.765781 False
2013-01-02 00:02:00 0.106163 False
2013-01-02 00:03:00 -1.169214 False
2013-01-02 00:04:00 0.224484 False
[10 rows x 2 columns]
In [28]: df['Active'] = False
正如@Andy Hayden 指出的那样,normalize
将时间设置为 0,以便您可以直接与时间为 0 的时间戳进行比较。
In [34]: df.loc[df.index.normalize() == Timestamp('20130102'),'Active'] = True
In [35]: df
Out[35]:
0 Active
2013-01-01 23:55:00 0.273194 False
2013-01-01 23:56:00 2.869795 False
2013-01-01 23:57:00 0.980566 False
2013-01-01 23:58:00 0.176711 False
2013-01-01 23:59:00 -0.354976 False
2013-01-02 00:00:00 0.258194 True
2013-01-02 00:01:00 -1.765781 True
2013-01-02 00:02:00 0.106163 True
2013-01-02 00:03:00 -1.169214 True
2013-01-02 00:04:00 0.224484 True
[10 rows x 2 columns]
要获得真正精细的控制,请执行此操作(如果您只想将 times 作为索引器,则可以使用 indexer_at_time
)。并且您始终可以使用 and 子句来进行更复杂的索引。
In [29]: df.loc[df.index.indexer_between_time('20130101 23:59:00','20130102 00:03:00'),'Active'] = True
In [30]: df
Out[30]:
0 Active
2013-01-01 23:55:00 0.273194 False
2013-01-01 23:56:00 2.869795 False
2013-01-01 23:57:00 0.980566 False
2013-01-01 23:58:00 0.176711 False
2013-01-01 23:59:00 -0.354976 True
2013-01-02 00:00:00 0.258194 True
2013-01-02 00:01:00 -1.765781 True
2013-01-02 00:02:00 0.106163 True
2013-01-02 00:03:00 -1.169214 True
2013-01-02 00:04:00 0.224484 False
[10 rows x 2 columns]
关于python - 按日期字符串选择 DataFrame 切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22699184/
简而言之: 我怎样才能切片?也就是说,能够指定我想从多个索引(例如 y = x[(2, 5, 11)] )中提取,而不仅仅是单个索引(例如 y = x[2] )。 简单示例 : 说我有这个数据: d
是否可以在 F# 中对 Array2D 进行切片? 说,let tmp =Array2D.init 100 100 (fun x y -> x * 100 + y) 如何从 tmp 中检索某些列或某些
例如,我希望文本仅显示“此处”,但它不起作用。文本经常变化,但我需要的单词保持在固定位置。我想访问该词。 我做错了什么? function myFunction() { var x = doc
当尝试使用spring的分页或切片来迭代非常大的mongodb集合时,程序运行正常,但在某些时候下一页/切片为空,并且在调试时出现“包含未知实例的页面/切片”消息. 这是代码示例: do { Pa
有人能给我一个关于如何分割 ListView 的例子吗?我正在使用 SimpleCursorAdapter 在 ListView 中显示数据.. 我的代码是这样的。 private WordDbAda
这个问题在这里已经有了答案: C++ slicing causing leak / undefined behavior / crash (3 个答案) 关闭 8 年前。 如果我有如下代码: cla
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 5 年前。 我目前有 500 行数据。我想使用前五十行,然后跳过 50 行,依此类推。我该如何继续这
为什么对一行或一列进行切片会产生“无量纲数组”?例如: import numpy as np arr = np.zeros((10,10)) print arr.shape # (10, 10) 但是
我有以下 pandas 数据框: Shortcut_Dimension_4_Code Stage_Code 10225003 2 8225003
如何通过数组为 ruby 中的散列创建切片,如下所示: info = { :key1 => "Lorem", :key2 => "something...", :key3 => "
这个问题在这里已经有了答案: regex to get all text outside of brackets (4 个答案) 关闭 5 年前。 我正在编写的这个程序接收到一个大小不同的字符串,其
我尝试使用 tf.Tensor.getitem 对张量进行切片功能如下: indices = [0, 5] data[:,:,indices] 但是我得到以下错误: TypeError: can on
这个问题在这里已经有了答案: Can I create a "view" on a Python list? (10 个答案) 关闭 7 年前。 有没有一种方法可以在 Python 3 中创建序列的
我想弄清楚如何从多维数组中获取单个维度(为了论证,假设它是二维的),我有一个多维数组: double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4,
我有一个 std::vector。我想创建代表该 vector 切片的迭代器。我该怎么做?在伪 C++ 中: class InterestingType; void doSomething(slice
写在前面 前面的文章介绍了Go的一些基本类型,本文开始涉及Go的一些容器类型,它们都是可以包含多个元素的数据结构,如数组、切片、map 数组 数组是具有相同类型且长度固定的一组元素集合,定义的格式:v
给定一个 numpy 数组和一个 __getitem__ 类型的索引,是否有一种惯用的方法来获取数组的相应切片,总是返回一个数组而不是标量? 有效索引的示例包括:int、slice、省略号或上述的元组
我创建了一个继承自 pandas.DataFrame 的类。在此类中添加了元数据(不是添加到列中,而是添加到类实例中): class MeasurementPoint(pandas.DataFrame
我想在空间上剪切视频以生成 N x M 个文件。 例如,我想把 test.video 拆分成 NxM 的瓦片? Video tiles 最佳答案 您可以使用 ffmpeg 及其 crop filter
这是一个示例代码。比如我想拉德国 在页面加载时切片。在这段代码中,它拉取第一个切片。 无功图; var 传说; var chartData = [{ 国家:“立陶宛”, 值:260}, { 国家:“爱
我是一名优秀的程序员,十分优秀!