Hướng dẫn convert datetime to int python pandas - chuyển đổi datetime sang int python pandas

Xem xét một khung dữ liệu gấu trúc trong Python có một cột có tên

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
2 của số nguyên loại, tôi có thể chuyển đổi nó thành định dạng
import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
3 với hướng dẫn sau.

df['time'] = pandas.to_datetime[df['time'], unit='s']

Vì vậy, bây giờ cột có các mục như:

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
4.

Lệnh để hoàn nguyên chuỗi thành giá trị dấu thời gian số nguyên là gì [biểu thị số giây đã trôi qua từ

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
5] là gì?

Tôi đã kiểm tra

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
6 nhưng không thể tìm thấy tiện ích chuyển đổi và tôi không thể sử dụng
import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
7 cho việc này.

Có bất kỳ tiện ích cho chuyển đổi này?

Đã hỏi ngày 22 tháng 1 năm 2019 lúc 16:43Jan 22, 2019 at 16:43

Francesco Boifrancesco BoiFrancesco Boi

7.52412 Huy hiệu vàng69 Huy hiệu bạc109 Huy hiệu đồng12 gold badges69 silver badges109 bronze badges

1

Bạn có thể đánh máy vào int bằng cách sử dụng

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
8 và chia nó cho
import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]
9 để có được số giây để bắt đầu Epoch Unix.

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
print[df_unix_sec]

Đã trả lời ngày 22 tháng 1 năm 2019 lúc 16:51Jan 22, 2019 at 16:51

Luôn nắng nắngAlways Sunny

34.2k8 Huy hiệu vàng52 Huy hiệu bạc90 Huy hiệu Đồng8 gold badges52 silver badges90 bronze badges

9

Cách dễ nhất là sử dụng

pd.to_datetime['1970-01-01'].value
0

pd.to_datetime['1970-01-01'].value

Nếu bạn muốn áp dụng nó cho toàn bộ cột, chỉ cần sử dụng

pd.to_datetime['1970-01-01'].value
1:

df['time'] = df['time'].apply[lambda x: x.value]

Đã trả lời ngày 14 tháng 11 năm 2019 lúc 11:39Nov 14, 2019 at 11:39

0

Sử dụng

pd.to_datetime['1970-01-01'].value
2 trên
pd.to_datetime['1970-01-01'].value
3:

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]

# pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
[df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]

Đầu ra

0    1.547559e+09
Name: time, dtype: float64

Đã trả lời ngày 22 tháng 1 năm 2019 lúc 16:51Jan 22, 2019 at 16:51

Luôn nắng nắngALollz

34.2k8 Huy hiệu vàng52 Huy hiệu bạc90 Huy hiệu Đồng7 gold badges59 silver badges79 bronze badges

Cách dễ nhất là sử dụng

pd.to_datetime['1970-01-01'].value
0

df['time'] = df['time'].apply[lambda x: x.value]

Nếu bạn muốn áp dụng nó cho toàn bộ cột, chỉ cần sử dụng

pd.to_datetime['1970-01-01'].value
1:

df['time'] = df['time'].apply[pd.Timestamp]

Đã trả lời ngày 14 tháng 11 năm 2019 lúc 11:39Nov 10, 2020 at 13:43

Sử dụng

pd.to_datetime['1970-01-01'].value
2 trên
pd.to_datetime['1970-01-01'].value
3:Jared Marks

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]

# pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
[df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
8 silver badges13 bronze badges

Đầu ra

import pandas as pd
df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
df_unix_sec = pd.to_datetime[df['time']].view[int] // 10 ** 9
print[df_unix_sec]

Alollzalollz

FutureWarning: casting datetime64[ns] values to int64 with .astype[...] is deprecated and will raise in a future version. Use .view[...] instead.

56.1K7 Huy hiệu vàng59 Huy hiệu bạc79 Huy hiệu đồngOct 27, 2021 at 9:40

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Python cung cấp một mô -đun gọi là DateTime để thực hiện tất cả các hoạt động liên quan đến ngày và giờ. Nó có một bộ chức năng phong phú được sử dụng để thực hiện hầu hết các hoạt động liên quan đến thời gian. Nó cần được nhập khẩu trước để sử dụng các chức năng và nó đi kèm với Python, vì vậy không cần phải cài đặt riêng.

    Ở đây, chúng tôi đối phó với một đối tượng ngày đặc biệt. Vì vậy, để chuyển đổi ngày đã cho thành số nguyên, chúng ta có thể làm theo phương thức sau.

    Python3

    Phương pháp 1: & nbsp; sử dụng phép nhân với 100 Lừa

    Trong phương pháp này, chúng tôi sẽ nhân mỗi thành phần của ngày với bội số 100 và thêm tất cả để chuyển đổi chúng thành các số nguyên.

    df['time'] = df['time'].apply[lambda x: x.value]
    
    1
    df['time'] = df['time'].apply[lambda x: x.value]
    
    2
    df['time'] = df['time'].apply[lambda x: x.value]
    
    3
    df['time'] = df['time'].apply[lambda x: x.value]
    
    4

    df['time'] = df['time'].apply[lambda x: x.value]
    
    1
    df['time'] = df['time'].apply[lambda x: x.value]
    
    2
    df['time'] = df['time'].apply[lambda x: x.value]
    
    7
    df['time'] = df['time'].apply[lambda x: x.value]
    
    8

    pd.to_datetime['1970-01-01'].value
    
    6
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
    print[df_unix_sec]
    
    3

    pd.to_datetime['1970-01-01'].value
    
    8
    pd.to_datetime['1970-01-01'].value
    
    9
    df['time'] = df['time'].apply[lambda x: x.value]
    
    0

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    0
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    1
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    2
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    3

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    5
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    1
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    7
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    3

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    0    1.547559e+09
    Name: time, dtype: float64
    
    0
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    1
    0    1.547559e+09
    Name: time, dtype: float64
    
    2
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    3

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    df['time'] = df['time'].apply[lambda x: x.value]
    
    5

    Output:

    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
    print[df_unix_sec]
    
    0

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    0    1.547559e+09
    Name: time, dtype: float64
    
    5
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    1
    0    1.547559e+09
    Name: time, dtype: float64
    
    7
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    3

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    df['time'] = df['time'].apply[lambda x: x.value]
    
    0
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    1
    df['time'] = df['time'].apply[lambda x: x.value]
    
    2
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    
    # pd.to_timedelta[df.time].dt.total_seconds[] # Is deprecated
    [df.time - pd.to_datetime['1970-01-01']].dt.total_seconds[]
    
    3

    Phương pháp 2: Sử dụng đối tượng DateTime.strftime []strftime[format]

    Trong phương thức này, chúng tôi đang sử dụng hàm strftime [] của lớp DateTime chuyển đổi nó thành chuỗi có thể được chuyển đổi thành một số nguyên bằng hàm int []. It returns the string representation of the date or time object.

    Code:

    Python3

    Phương pháp 1: & nbsp; sử dụng phép nhân với 100 Lừa

    Trong phương pháp này, chúng tôi sẽ nhân mỗi thành phần của ngày với bội số 100 và thêm tất cả để chuyển đổi chúng thành các số nguyên.

    df['time'] = df['time'].apply[lambda x: x.value]
    
    1
    df['time'] = df['time'].apply[lambda x: x.value]
    
    2
    df['time'] = df['time'].apply[lambda x: x.value]
    
    3
    df['time'] = df['time'].apply[lambda x: x.value]
    
    4

    df['time'] = df['time'].apply[lambda x: x.value]
    
    1
    df['time'] = df['time'].apply[lambda x: x.value]
    
    2
    df['time'] = df['time'].apply[lambda x: x.value]
    
    7
    df['time'] = df['time'].apply[lambda x: x.value]
    
    8

    df['time'] = df['time'].apply[lambda x: x.value]
    
    9
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].view[int] // 10 ** 9
    print[df_unix_sec]
    
    0
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].view[int] // 10 ** 9
    print[df_unix_sec]
    
    1
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].view[int] // 10 ** 9
    print[df_unix_sec]
    
    2
    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].view[int] // 10 ** 9
    print[df_unix_sec]
    
    3

    Output:

    import pandas as pd
    df = pd.DataFrame[{'time': [pd.to_datetime['2019-01-15 13:25:43']]}]
    df_unix_sec = pd.to_datetime[df['time']].astype[int]/ 10**9
    print[df_unix_sec]
    
    1

    Làm cách nào để chuyển đổi DateTime thành INT trong Python?

    đối tượng strftime []. Trong phương thức này, chúng tôi đang sử dụng hàm strftime [] của lớp DateTime chuyển đổi nó thành chuỗi có thể được chuyển đổi thành một số nguyên bằng hàm int []. Trả về: Nó trả về biểu diễn chuỗi của đối tượng ngày hoặc thời gian.. In this method, we are using strftime[] function of datetime class which converts it into the string which can be converted to an integer using the int[] function. Returns : It returns the string representation of the date or time object.

    Làm cách nào để chuyển đổi dấu thời gian thành một số trong Python?

    Ví dụ 1: Thời gian số nguyên của ngày và thời gian hiện tại Chuyển đổi đối tượng DateTime thành dấu thời gian bằng DateTime.Phương thức dấu thời gian [].Chúng tôi sẽ nhận được dấu thời gian trong vài giây.Và sau đó làm tròn dấu thời gian và đánh máy rõ ràng số điểm nổi vào một số nguyên để có được dấu thời gian số nguyên trong vài giây.Convert the DateTime object into timestamp using DateTime. timestamp[] method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

    Làm thế nào để bạn chuyển đổi một đối tượng DateTime thành một chuỗi trong Python?

    Để chuyển đổi DateTime thành chuỗi trong Python, hãy sử dụng hàm strftime [].Phương thức strftime [] là một phương thức tích hợp, trả về chuỗi biểu diễn ngày và thời gian bằng cách sử dụng đối tượng ngày, giờ hoặc dữ liệu.use the strftime[] function. The strftime[] method is a built-in method that returns the string representing date and time using date, time, or datetime object.

    Làm cách nào để chuyển đổi DateTime thành giây trong Python?

    Để chuyển đổi DateTime thành Seconds, hãy trừ Datetime đầu vào từ thời đại Epoch.Đối với Python, thời gian kỷ nguyên bắt đầu lúc 00:00:00 UTC vào ngày 1 tháng 1 năm 1970. Phép trừ cung cấp cho bạn đối tượng Timedelta.Sử dụng phương thức Total_Seconds [] của một đối tượng TimeDelta để có được số giây kể từ kỷ nguyên.subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds[] method of a timedelta object to get the number of seconds since the epoch.

    Bài Viết Liên Quan

    Chủ Đề