Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SettingWithCopyWarning] for getting ATR #38

Closed
yurenji opened this issue Jul 28, 2018 · 7 comments
Closed

[SettingWithCopyWarning] for getting ATR #38

yurenji opened this issue Jul 28, 2018 · 7 comments

Comments

@yurenji
Copy link

yurenji commented Jul 28, 2018

my code:

    @classmethod
    def get_atr(cls, candles):
        stock = StockDataFrame.retype(TCandle.to_df(candles))
        return list(stock.get('atr'))

I got the following warning:

  /Users/yurenji/.conda/envs/tangle/lib/python3.6/site-packages/pandas/core/indexing.py:189: SettingWithCopyWarning: 
  A value is trying to be set on a copy of a slice from a DataFrame
  
  See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
    self._setitem_with_indexer(indexer, value)

-- Docs: http://doc.pytest.org/en/latest/warnings.html

I tried 'macdh' and 'sma', they don't have this issue.

@erikgqp8645
Copy link

Hello, your problem has been solved.
I've had the same problem as you, and I think I've probably found it

@jealous
Copy link
Owner

jealous commented Oct 16, 2018

Hi @erikgqp8645 , could you share the solution?

@erikgqp8645
Copy link

你好!
@jealous
我看到你是上海的,并且看到里面有人讲中文,就冒昧的讲中文试下,因为我的英文实在很差劲。
我想是我高兴的太早了,我并没有找到问题发生的根本原因,或许是距离真相更近一步了。
我觉得问题可能发生在数据上,我把我的数据和结果都黏贴上来,希望我们一起解决他。

原始代码是这样,请原谅我不会插入代码
import math
import pandas as pd
import numpy as np
import tushare as ts
import datetime
import matplotlib.pyplot as plt
import stockstats

设置做大显示列和行,中间不显示省略号

pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', 1000)
pd.set_option('display.width', 1000) # 设置多列不换行

读取兔子上的数据

begin_time = '2017-02-01'
end_time = '2017-11-01'
code = "000001"
stock = ts.get_hist_data(code, start=begin_time, end=end_time)
stock["date"] = stock.index.values # 增加日期列。
stock = stock.sort_index(0) # 将数据按照日期排序下。

初始化统计类

stockStat = stockstats.StockDataFrame.retype(stock)
stockStat._get_atr(stockStat, window=20)
print(stock)

读取df的数据存在问题

df = pd.read_csv("E:\Text_Guo\Data\btc\BITFINEX_BTCUSD_20180101_1T.csv", skiprows=1)

stock["date"] = stock.index.values # 增加日期列。

df = stockstats.sort_index(0) # 将数据按照日期排序下。

初始化统计类

stockStat_df = stockstats.StockDataFrame.retype(df)
stockStat_df._get_atr(stockStat_df, window=20)
print(stockStat_df)

第一个结果是这样
open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20 close_-1_s tr atr_20
date
2017-02-02 9.45 10.26 10.26 9.33 19345.00 0.93 9.97 9.468 9.320 9.234 327000.59 426903.19 397560.55 NaN NaN NaN
2017-02-03 9.34 9.36 9.26 9.23 315472.25 -0.07 -0.75 9.476 9.332 9.242 306035.17 390133.84 398203.79 10.26 1.03 1.030000
2017-02-06 9.26 9.32 9.31 9.26 516786.12 0.05 0.54 9.484 9.348 9.249 315343.58 387257.21 401051.07 9.26 0.06 0.532564
2017-02-07 9.31 9.32 9.30 9.27 396884.97 -0.01 -0.11 9.492 9.361 9.256 333840.18 369518.77 398428.85 9.31 0.05 0.363392
2017-02-08 9.29 9.30 9.30 9.24 360272.41 0.00 0.00 9.486 9.373 9.263 321752.15 361774.72 399223.82 9.30 0.06 0.281612
2017-02-09 9.30 9.33 9.31 9.28 342855.12 0.01 0.11 9.296 9.382 9.272 386454.17 356727.38 398458.87 9.30 0.05 0.230420
2017-02-10 9.32 9.36 9.33 9.31 482743.25 0.02 0.21 9.310 9.393 9.281 419908.37 362971.77 404541.95 9.31 0.05 0.196367
2017-02-13 9.34 9.44 9.41 9.33 638364.75 0.08 0.86 9.330 9.407 9.294 444224.10 379783.84 424407.49 9.33 0.11 0.182052
2017-02-14 9.41 9.42 9.40 9.37 362404.59 -0.01 -0.11 9.350 9.421 9.307 437328.02 385584.10 427356.18 9.41 0.05 0.162435
2017-02-15 9.40 9.54 9.45 9.39 756613.06 0.05 0.53 9.380 9.433 9.322 516596.15 419174.15 443786.49 9.40 0.15 0.160753
2017-02-16 9.45 9.50 9.46 9.42 411161.12 0.01 0.11 9.410 9.353 9.337 530257.35 458355.76 442629.48 9.45 0.08 0.150691
2017-02-17 9.46 9.49 9.39 9.37 423774.97 -0.07 -0.74 9.422 9.366 9.349 518463.70 469186.04 429659.94 9.46 0.12 0.147132
2017-02-20 9.40 9.58 9.56 9.40 898755.38 0.17 1.81 9.452 9.391 9.370 570541.82 507382.96 447320.09 9.39 0.19 0.151795
2017-02-21 9.55 9.62 9.57 9.54 646584.19 0.01 0.10 9.486 9.418 9.390 627377.74 532352.88 450935.83 9.56 0.08 0.144419
2017-02-22 9.57 9.57 9.57 9.50 462966.12 0.00 0.00 9.510 9.445 9.409 568648.36 542622.26 452198.49 9.57 0.07 0.137156
2017-02-23 9.55 9.57 9.51 9.48 335327.81 -0.06 -0.63 9.520 9.465 9.424 553481.69 541869.52 449298.45 9.57 0.09 0.132763
2017-02-24 9.50 9.54 9.50 9.48 332500.12 -0.01 -0.10 9.542 9.482 9.438 535226.72 526845.21 444908.49 9.51 0.06 0.126265
2017-02-27 9.50 9.50 9.43 9.42 407341.12 -0.07 -0.74 9.516 9.484 9.446 436943.87 503742.85 441763.34 9.50 0.08 0.122289
2017-02-28 9.43 9.51 9.48 9.42 369719.69 0.05 0.53 9.498 9.492 9.457 381570.97 504474.36 445029.23 9.43 0.09 0.119611
2017-03-01 9.49 9.55 9.49 9.47 346993.72 0.01 0.10 9.482 9.496 9.465 358376.49 463512.42 441343.29 9.48 0.08 0.116430
2017-03-02 9.51 9.54 9.43 9.42 403628.72 -0.06 -0.63 9.466 9.493 9.423 372036.67 462759.18 460557.47 9.49 0.12 0.116708
2017-03-03 9.41 9.43 9.40 9.36 342655.09 -0.03 -0.32 9.446 9.494 9.430 374067.67 454647.20 461916.62 9.43 0.07 0.113167
2017-03-06 9.40 9.46 9.45 9.39 404511.38 0.05 0.53 9.450 9.483 9.437 373501.72 405222.80 456302.88 9.40 0.07 0.109976
2017-03-07 9.44 9.46 9.45 9.40 294672.81 0.00 0.00 9.444 9.471 9.445 358492.34 370031.66 451192.27 9.45 0.06 0.106369
2017-03-08 9.43 9.45 9.42 9.40 244438.41 -0.03 -0.32 9.430 9.456 9.451 337981.28 348178.89 445400.57 9.45 0.05 0.102388
2017-03-09 9.41 9.43 9.38 9.36 378169.91 -0.04 -0.42 9.420 9.443 9.454 332889.52 352463.10 447166.31 9.42 0.07 0.100147
2017-03-10 9.38 9.41 9.40 9.36 390182.25 0.02 0.21 9.420 9.433 9.458 342394.95 358231.31 442538.26 9.38 0.05 0.096742
2017-03-13 9.39 9.45 9.44 9.37 545304.44 0.04 0.43 9.418 9.434 9.459 370553.56 372027.64 437885.25 9.40 0.08 0.095626
2017-03-14 9.43 9.46 9.44 9.41 404484.38 0.00 0.00 9.416 9.430 9.461 392515.88 375504.11 439989.23 9.44 0.05 0.092633
2017-03-15 9.42 9.48 9.48 9.42 546560.88 0.04 0.42 9.428 9.429 9.463 452940.37 395460.83 429486.63 9.44 0.06 0.090525
2017-03-16 9.48 9.53 9.52 9.45 635953.81 0.04 0.42 9.456 9.438 9.466 504497.15 418693.34 440726.26 9.48 0.08 0.089855
2017-03-17 9.45 9.46 9.31 9.30 1583647.00 -0.21 -2.21 9.438 9.429 9.462 743190.10 542792.53 498719.86 9.52 0.22 0.098029
2017-03-20 9.29 9.31 9.25 9.22 715021.75 -0.06 -0.64 9.400 9.409 9.446 777133.56 573843.56 489533.18 9.31 0.09 0.097531
2017-03-21 9.25 9.26 9.24 9.20 554648.75 -0.01 -0.11 9.360 9.388 9.430 807166.44 599841.16 484936.41 9.25 0.06 0.095231
2017-03-22 9.20 9.22 9.16 9.14 566123.62 -0.08 -0.87 9.296 9.362 9.409 811078.99 632009.68 490094.28 9.24 0.10 0.095520

第二个结果是这样
D:\anaconda3\lib\site-packages\pandas\core\indexing.py:189: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.setitem_with_indexer(indexer, value)
candle_begin_time open high low close volume close
-1_s tr atr_20
0 2018-01-01 00:00:00 13763.00000 13788.000000 13752.182339 13753.00000 5.133268 NaN NaN NaN
1 2018-01-01 00:01:00 13763.00000 13773.000000 13741.000000 13772.00000 15.536082 13753.00000 32.000000 32.000000
2 2018-01-01 00:02:00 13774.00000 13777.000000 13741.000000 13742.00000 16.958926 13772.00000 36.000000 34.051282
3 2018-01-01 00:03:00 13741.00000 13742.000000 13721.000000 13722.00000 25.783042 13742.00000 21.000000 29.475898
4 2018-01-01 00:04:00 13722.00000 13722.000000 13709.000000 13709.00000 11.431000 13722.00000 13.000000 25.034806
5 2018-01-01 00:05:00 13710.00000 13710.000000 13559.000000 13628.00000 169.517133 13709.00000 151.000000 52.876225
6 2018-01-01 00:06:00 13628.00000 13637.000000 13621.000000 13625.00000 21.351554 13628.00000 16.000000 45.916033
7 2018-01-01 00:07:00 13624.00000 13624.000000 13591.000000 13607.00000 23.800193 13625.00000 34.000000 43.940974
8 2018-01-01 00:08:00 13602.00000 13647.000000 13602.000000 13639.00000 26.622142 13607.00000 45.000000 44.098296
9 2018-01-01 00:09:00 13639.00000 13676.716245 13607.000000 13640.00000 45.154382 13639.00000 69.716245 47.562516
10 2018-01-01 00:10:00 13639.00000 13641.000000 13600.000000 13640.00000 36.542425 13640.00000 41.000000 46.744784
11 2018-01-01 00:11:00 13640.00000 13650.000000 13624.000000 13624.00000 26.901903 13640.00000 26.000000 44.339312
12 2018-01-01 00:12:00 13624.00000 13637.143107 13611.960201 13615.00000 8.482073 13624.00000 25.182906 42.255463

@erikgqp8645
Copy link

第一个数据是没有问题的,第二个数据存在问题。

@jealous
Copy link
Owner

jealous commented Oct 17, 2018

我也不清楚是哪个操作触发了这个警告。你可以忽略这个警告,目前看起来不会对数据有影响。

@erikgqp8645
Copy link

好的,我试着忽略他。
谢谢。

@jealous
Copy link
Owner

jealous commented Oct 17, 2020

Fixed in 0.3.2.

@jealous jealous closed this as completed Oct 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants