[P/M/K] simple way to downcast type to reduce memory

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 11 columns):
Season_Year    5 non-null int64
GameKey        5 non-null int64
PlayID         5 non-null int64
GSISID         5 non-null float64
Time           5 non-null object
x              5 non-null float64
y              5 non-null float64
dis            5 non-null float64
o              5 non-null float64
dir            5 non-null float64
Event          0 non-null float64
dtypes: float64(7), int64(3), object(1)
memory usage: 520.0+ bytes
# Find out the smallest data type possible for each numeric feature
float_cols = df_temp.select_dtypes(include=['float'])
int_cols = df_temp.select_dtypes(include=['int'])

for cols in float_cols.columns:
    df_temp[cols] = pd.to_numeric(df_temp[cols], downcast='float')
    
for cols in int_cols.columns:
    df_temp[cols] = pd.to_numeric(df_temp[cols], downcast='integer')

print(df_temp.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 11 columns):
Season_Year    5 non-null int16
GameKey        5 non-null int8
PlayID         5 non-null int16
GSISID         5 non-null float32
Time           5 non-null object
x              5 non-null float32
y              5 non-null float32
dis            5 non-null float32
o              5 non-null float32
dir            5 non-null float32
Event          0 non-null float32
dtypes: float32(7), int16(2), int8(1), object(1)
memory usage: 285.0+ bytes
None
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,Downcast 是指将一个父类对象转换为子类对象的过程。这种转换是不安全的,因为父类对象可能并不是子类对象,如果进行错误的 Downcast,可能会导致运行时异常的发生。 为了避免运行时异常的发生,需要在进行 Downcast 之前,先使用 instanceof 运算符来检查对象的类型是否正确,只有在确认对象的类型是正确的情况下,才进行 Downcast 操作。 下面是一个 Downcast 的例子: ``` class Parent { public void print() { System.out.println("Parent"); } } class Child extends Parent { public void sayHello() { System.out.println("Hello"); } } public class Test { public static void main(String[] args) { Parent p = new Child(); if (p instanceof Child) { Child c = (Child) p; // Downcast c.sayHello(); } } } ``` 在上面的例子中,先创建了一个 Child 对象,并将其赋值给 Parent 类型的变量 p。然后使用 instanceof 运算符检查 p 的类型是否为 Child 类型,如果是,则进行 Downcast 操作,将 p 转换为 Child 类型的变量 c,并调用 c 的 sayHello() 方法。由于 p 是 Child 类型的对象,因此 Downcast 操作是安全的。 需要注意的是,在进行 Downcast 操作时,需要使用强制类型转换符 (cast) 将父类对象转换为子类对象。如果父类对象并不是子类对象,就会引发 ClassCastException 异常。因此,在进行 Downcast 操作时,必须先使用 instanceof 运算符检查对象的类型是否正确,避免运行时异常的发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值