我想在pandas滚动功能中设置center = True,对于时间序列:
import pandas as pd
series = pd.Series(1, index = pd.date_range('2014-01-01', '2014-04-01', freq = 'D'))
series.rolling('7D', min_periods=1, center=True, closed='left')
Run Code Online (Sandbox Code Playgroud)
但输出是:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-6-6b30c16a2d12> in <module>()
1 import pandas as pd
2 series = pd.Series(1, index = pd.date_range('2014-01-01', '2014-04-01', freq = 'D'))
----> 3 series.rolling('7D', min_periods=1, center=True, closed='left')
~\Anaconda3\lib\site-packages\pandas\core\generic.py in rolling(self, window, min_periods, freq, center, win_type, on, axis, closed)
6193 min_periods=min_periods, freq=freq,
6194 center=center, win_type=win_type,
-> 6195 on=on, axis=axis, closed=closed)
6196
6197 cls.rolling = …
Run Code Online (Sandbox Code Playgroud) 我正在从 python 调用一个非常简单的 R 脚本,称为R Code.R:
args <- commandArgs(TRUE)
print(args)
source(args[1])
setwd("<YOUR PATH>")
output <- head(mtcars, n = n)
write.table(output, "output.txt")
Run Code Online (Sandbox Code Playgroud)
使用以下脚本:
import subprocess
pth = "<YOUR PATH>"
subprocess.call(" ".join(["C:/R/R-3.6.0/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
subprocess.call(" ".join(["C:/R/R-4.0.3/bin/x64/R.exe", "-f", '"' + pth + '/R Code.R"', "--args",
'"' + pth + '/arguments.txt"',"1>", '"' + pth + '/log.txt"', "2>&1"]))
Run Code Online (Sandbox Code Playgroud)
其中arguments.txt包含:n <- 10
问题是,当我使用 R-4.0.3 时,没有生成 …
当使用接近无穷大的浮点数时,我有一个大熊猫滚动的错误.我在这里显示一个例子:
import pandas as pd
series = pd.Series(1.,index = pd.date_range('2015-01-01', periods=6))
series[series.index[2]] = 1e19
series
2015-01-01 1.000000e+00
2015-01-02 1.000000e+00
2015-01-03 1.000000e+19
2015-01-04 1.000000e+00
2015-01-05 1.000000e+00
2015-01-06 1.000000e+00
Freq: D, dtype: float64
series.rolling('2D', closed = 'left').mean()
2015-01-01 NaN
2015-01-02 1.000000e+00
2015-01-03 1.000000e+00
2015-01-04 5.000000e+18
2015-01-05 5.000000e+18
2015-01-06 5.000000e-01
Freq: D, dtype: float64
Run Code Online (Sandbox Code Playgroud)
最后一点的答案应该是1!但它是0.5.为什么在使用大数字时滚动疯狂?相同的例子与较小的浮动:
series[series.index[2]] = 1e9
series.rolling('2D', closed = 'left').mean()
2015-01-01 NaN
2015-01-02 1.0
2015-01-03 1.0
2015-01-04 500000000.5
2015-01-05 500000000.5
2015-01-06 1.0
Freq: D, dtype: float64
Run Code Online (Sandbox Code Playgroud) 我想具有与快照相同的功能,但使用左侧出现的频率而不是最近的频率。
这是我正在尝试的:
date = pd.date_range('2015-01-01', '2015-12-31')
week_index = pd.DatetimeIndex.snap(date, 'W-MON')
week_index
DatetimeIndex(['2014-12-29', '2015-01-05', '2015-01-05', '2015-01-05',
'2015-01-05', '2015-01-05', '2015-01-05', '2015-01-05',
'2015-01-12', '2015-01-12',
...
'2015-12-21', '2015-12-21', '2015-12-21', '2015-12-28',
'2015-12-28', '2015-12-28', '2015-12-28', '2015-12-28',
'2015-12-28', '2015-12-28'],
dtype='datetime64[ns]', length=365, freq='W-MON')
Run Code Online (Sandbox Code Playgroud)
尽管它会输出ValueError,但似乎应该执行该操作:
week_index = pd.DatetimeIndex.floor(date, 'W-MON')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-a053b5230ee3> in <module>()
----> 1 week_index = pd.DatetimeIndex.floor(date, 'W-MON')
C:\ProgramData\Anaconda3\lib\site-packages\pandas\tseries\base.py in floor(self, freq)
99 @Appender(_round_doc % "floor")
100 def floor(self, freq):
--> 101 return self._round(freq, np.floor)
102
103 @Appender(_round_doc % "ceil") …
Run Code Online (Sandbox Code Playgroud)