简体   繁体   中英

datetime conversion and manipulation in python

My raw data is in CSV. I load it as a pandas dataframe and datetime fields are loaded as objects.

datetime1   22773 non-null object
datetime2   22771 non-null object

Using pd.to_datetime(df['datetime1']) I convert it to - datetime64[ns] .

But in doing so the actual value is increased by 7 hours.

I have 2 questions -

  1. What is the unit datetime64[ns] ? is it based on unix time or some other time zone?

  2. How can I subtract the 7 hours and keep the actual value but my field format is still datetime?

  1. It's just a data type that's based on numpy's datetime64[ns]. It doesn't contain a timezone attribute that altered your data

  2. df["existing or new column"] = df["datetime1] - pd.Timedelta(7, 'h')

Also, you can always convert to date time when you read the csv using the parse_dates parameter like so. That way to can skip the pd.to_datetime() step

df = pd.read_csv("filename", parse_dates = ["datetime1","datetime2"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM