小编Del*_*ore的帖子

Numpy在2D矩阵上的where()

我有一个像这样的矩阵

t = np.array([[1,2,3,'foo'],
 [2,3,4,'bar'],
 [5,6,7,'hello'],
 [8,9,1,'bar']])
Run Code Online (Sandbox Code Playgroud)

我想得到行包含字符串'bar'的索引

在1d数组中

rows = np.where(t == 'bar')
Run Code Online (Sandbox Code Playgroud)

应该给我指数[0,3]然后广播: -

results = t[rows]
Run Code Online (Sandbox Code Playgroud)

应该给我正确的行

但我无法弄清楚如何让它与2d数组一起工作.

python arrays numpy matrix

17
推荐指数
2
解决办法
3万
查看次数

禁用Cargo中的注册表更新

如何禁用cargo updatecargo build尝试访问github.com; 但仍然从中下载相应的包crates.io

我的cargo.toml有一个依赖

[dependencies]
chrono = "0.2.14"
Run Code Online (Sandbox Code Playgroud)

运行 cargo build

E:\>cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
Unable to update registry https://github.com/rust-lang/crates.io-index
Run Code Online (Sandbox Code Playgroud)

我们在工作时 github.com 阻止,但不是crates.io.有没有选项,货物仍然可以下载所需的包,而无需更新它的注册表?

rust-cargo

12
推荐指数
1
解决办法
2814
查看次数

使用Drop trait释放repr(C)结构的正确习惯用法

这段代码工作正常,但在Rust nightly(1.2)上给出了一个编译器警告

#[repr(C)]
struct DbaxCell { 
    cell: *const c_void
}

#[link(name="CDbax", kind="dylib")] 
extern {
    fn new_dCell(d: c_double) -> *const c_void;
    fn deleteCell(c: *const c_void);
}

impl DbaxCell {
    fn new(x: f64) -> DbaxCell {
        unsafe {
            DbaxCell { cell: new_dCell(x) }
        }
    }
}

impl Drop for DbaxCell {
    fn drop(&mut self) {
        unsafe {
            deleteCell(self.cell);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它链接到C库并正确创建/删除单元格对象.但它会发出警告

src\lib.rs:27:1: 33:2 warning: implementing Drop adds hidden state to types, possibly conflicting with `#[repr(C)]`, #[warn(drop_with_repr_extern)] on by default
\src\lib.rs:27 impl …
Run Code Online (Sandbox Code Playgroud)

c memory rust

11
推荐指数
1
解决办法
310
查看次数

无法在pandas数据帧中按时间戳索引

我拿了一张excel表,其中包含日期和一些值,并希望将它们转换为pandas数据帧,并仅选择某些日期之间的行.

出于某种原因,我不能选择按日期索引的行

Excel文件中的原始数据

MCU                         
Timestamp   50D 10P1    10P2    10P3    10P6    10P9    10P12
12-Feb-15   25.17   5.88    5.92    5.98    6.18    6.23    6.33
11-Feb-15   25.9    6.05    6.09    6.15    6.28    6.31    6.39
10-Feb-15   26.38   5.94    6.05    6.15    6.33    6.39    6.46
Run Code Online (Sandbox Code Playgroud)

xls = pd.ExcelFile('e:/Data.xlsx')
vols = xls.parse(asset.upper()+'VOL',header=1)
vols.set_index('Timestamp',inplace=True)
Run Code Online (Sandbox Code Playgroud)

set_index之前的数据

      Timestamp    50D  10P1  10P2  10P3  10P6  10P9  10P12  25P1  25P2  \
0    2015-02-12  25.17  5.88  5.92  5.98  6.18  6.23   6.33  2.98  3.08   
1    2015-02-11  25.90  6.05  6.09  6.15  6.28  6.31   6.39  3.12  3.17   
2    2015-02-10 …
Run Code Online (Sandbox Code Playgroud)

python indexing excel dataframe pandas

6
推荐指数
1
解决办法
4343
查看次数

用日期时间索引插值并填充熊猫数据框

嗨,我正在尝试插入一个具有datetimeIndex索引的数据框。

这是数据

res = pd.DataFrame(cursor.execute("SELECT DATETIME,VALUE FROM {} WHERE DATETIME > ? AND DATETIME < ?".format(table),[start,end]).fetchall(),columns=['date','value'])
res.set_index('date',inplace=True)
Run Code Online (Sandbox Code Playgroud)

产生

2013-01-31 00:00:00   517  
2012-12-31 00:00:00   263  
2012-11-30 00:00:00  1917  
2012-10-31 00:00:00   391  
2012-09-30 00:00:00   782  
2012-08-31 00:00:00   700  
2012-07-31 00:00:00   799  
2012-06-30 00:00:00   914  
2012-05-31 00:00:00   141  
2012-04-30 00:00:00   342  
2012-03-31 00:00:00   199  
2012-02-29 00:00:00   533  
2012-01-31 00:00:00  1393  
2011-12-31 00:00:00   497  
2011-11-30 00:00:00  1457  
2011-10-31 00:00:00   997  
2011-09-30 00:00:00   533  
2011-08-31 00:00:00   626  
2011-07-31 00:00:00  1933  
2011-06-30 00:00:00  4248  
2011-05-31 00:00:00 …
Run Code Online (Sandbox Code Playgroud)

python pandas

6
推荐指数
2
解决办法
9477
查看次数

链接到构建文件夹外的Windows库

有没有办法链接不在当前包路径中的库.

链接建议将所有内容放在本地目录下.我们的包安装在其他地方的某个存储库中 我只想在windows上指定它的libpath.

authors = ["Me"]
links = "CDbax"

[target.x86_64-pc-windows-gnu.CDbax]
rustc-link-lib = ["CDbax"]
rustc-link-search = ["Z:/Somepath//CPP/CDbax/x64/Debug/"]
root = "Z:/Somepath//CPP/CDbax/x64/Debug/"
Run Code Online (Sandbox Code Playgroud)

但尝试货物建造-v给了我

package `hello v0.1.0 (file:///H:/Users/Mushfaque.Cradle/Documents/Rustc/hello)` specifies that it links to `CDbax` but does not have a custom build script
Run Code Online (Sandbox Code Playgroud)

从货物构建脚本支持指南,它似乎表明这应该工作.但我可以看到它没有添加路径.bin\x68_64-pc-windows-gnu\然而,将lib移动到本地路径中是有效的.

更新 感谢下面的答案,我想我会更新这个,以便给出我的机器上有效的最终结果,以便其他人发现它很有用.

在Cargo.toml中添加

links = "CDbax"
build = "build.rs"
Run Code Online (Sandbox Code Playgroud)

即使没有build.rs文件,它似乎需要它(?)否则抱怨

package `xxx v0.1.0` specifies that it links to `CDbax` but does not have a custom build script
Run Code Online (Sandbox Code Playgroud)

随后Vaelden回答在.cargo中创建一个'配置'文件

如果这是一个子包,你不需要将links =标记放在父包中,即使它是一个dll; 即使是'货运'.我假设它将dll路径添加到执行环境中

windows ffi rust

6
推荐指数
1
解决办法
2451
查看次数

为什么我必须在客户端库中公开宏实现的"使用"?

我正在尝试使用我在单独模块中创建的宏.参考这个SO问题,我已经导入了一个宏.但似乎我有

更新以添加宏实现

lib.rs

#![macro_use]
use std::fmt;
use std::ffi::CString;
use std::ffi::CStr;
use std::str;
extern crate libc;

pub enum DbaxError {
    DBAXException(String)
}

 #[macro_export]
macro_rules! dbax_call_test {
    ( $func : expr ) => {
        {
            let call_c_func = unsafe { dbax_function(CString::new($func).unwrap().as_ptr(),0) };
            match unsafe { getErrorCode() as i32 } {
                0 => Ok("Done".to_owned() + $func),
                _ => Err(DbaxError::DBAXException( str::from_utf8(unsafe { CStr::from_ptr(getError()) }.to_bytes()).unwrap().to_string()))
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

和主人在一个单独的箱子里

// Import macro
#[macro_use] extern crate rustdbax;
// Import implementation details of macro …
Run Code Online (Sandbox Code Playgroud)

macros rust

5
推荐指数
1
解决办法
402
查看次数

在 Windows 上代理后面的货物 ssl 下载错误

我无法让货物在经过身份验证的代理后面的 Windows 下开始任何下载。

这是我的代理设置:-

C:\Users\ukb99427\Downloads
? set | grep http
https_proxy=http://user:pass@corporate.proxy:8080
http_proxy=http://user:pass@corporate.proxy:8080
Run Code Online (Sandbox Code Playgroud)

注意 http s _proxy 有一个http地址。这允许像 git 这样的东西,顺便说一下 rustup-init 和 rustup 可以正常工作。这些输出是

? rustup update
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: syncing channel updates for 'nightly-x86_64-pc-windows-msvc'
info: latest update on 2017-11-10, rust version 1.23.0-nightly (d6b06c63a 2017-11-09)
info: downloading component 'rustc'
 33.4 MiB /  33.4 MiB (100 %)   2.7 MiB/s ETA:   0 s
Run Code Online (Sandbox Code Playgroud)

但是当运行等效cargo install命令时,我得到以下信息

? cargo install libc
    Updating registry `https://github.com/rust-lang/crates.io-index`
warning: spurious network …
Run Code Online (Sandbox Code Playgroud)

ssl rust rust-cargo

5
推荐指数
1
解决办法
2681
查看次数

有没有办法在宏中实现可选表达式?

我有一个宏,可以简化从字符串中解析枚举的过程:

enum_printer!(Bar { fun1["AB","CD"]; fun2[] });
Run Code Online (Sandbox Code Playgroud)

为了使其更容易使用,我想让[]可选的 ie 能够指定

enum_printer!(Bar { fun1["AB","CD"]; fun2; fun3 });
Run Code Online (Sandbox Code Playgroud)

这是现在的代码

use failure::Error;
use std::convert::TryFrom;

macro_rules! enum_printer {
    ($name:ident { $($enumeration:ident [$($v:expr),*]);* }) => {
        #[derive(Debug)]
        enum $name {
            $(
                $enumeration,
            )*
        }
        impl<'a> TryFrom<&'a str> for $name {
            type Error = Error;
            fn try_from(value : &'a str) -> Result<$name,Error> {
            match value {
                $(
                    stringify!($enumeration) 
                    $(
                        | $v
                    )*                        
                    => Ok($name::$enumeration),
                )*
                _ => Err(err_msg("Could not convert from string to"))
            }                
            } …
Run Code Online (Sandbox Code Playgroud)

macros parsing rust

5
推荐指数
1
解决办法
1139
查看次数

为什么 `impl Trait` 返回值实现 Send 而 `Box&lt;dyn Trait&gt;` 没有?

How do I store a variable of type `impl Trait` in a struct? 中的解决方案建议创建一个Futuretrait 对象。在我的真实代码中这样做会产生一个错误,即类型不是Send,但工作版本和非工作版本之间的唯一区别是是否存在强制转换为dyn Future.

为什么编译器认为这些不同,我该如何解决这个问题?

这是问题的简化版本:

use std::future::Future;

fn uses_impl_trait() -> impl Future<Output = i32> {
    async { 42 }
}

fn uses_trait_object() -> Box<dyn Future<Output = i32>> {
    Box::new(async { 42 })
}

fn requires_send<T: Send>(_: T) {}

fn example() {
    requires_send(uses_impl_trait()); // Works
    requires_send(uses_trait_object()); // Fails
}
Run Code Online (Sandbox Code Playgroud)
use std::future::Future;

fn uses_impl_trait() -> impl Future<Output = i32> { …
Run Code Online (Sandbox Code Playgroud)

traits rust

5
推荐指数
1
解决办法
492
查看次数

标签 统计

rust ×6

python ×3

macros ×2

pandas ×2

rust-cargo ×2

arrays ×1

c ×1

dataframe ×1

excel ×1

ffi ×1

indexing ×1

matrix ×1

memory ×1

numpy ×1

parsing ×1

ssl ×1

traits ×1

windows ×1