Convert a Tuple into Datetime

Tuple to datetime object

Just get the first element of the tuple, which is a datetime object already.

time = my_tuple[0]

Python27 - Convert tuple time to datetime object

email.utils.parsedate returns a 9 tuple similar to the structure struct_time but with the index 6,7 and 8 unusable

struct_time:

Index   Attribute   Values
0 tm_year (for example, 1993)
1 tm_mon range [1, 12]
2 tm_mday range [1, 31]
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]; see (2) in strftime() description
6 tm_wday range [0, 6], Monday is 0
7 tm_yday range [1, 366]
8 tm_isdst 0, 1 or -1

And datetime objects require different values for its constructor

datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

You could directly create a datetime using the useful parts of your tuple as

date_object = datetime(*d[0:6])


Edit: Careful with this, because this will create the object in local time, disregarding the time zone information.


Edit 2: You can solve this by using strptime, you just need to cut the (PDT) from the end of your string, since PDT is not a valid name for tzinfo, but -0700 is enough

convert datetime type to tuple in python

If you want to "customize" the output (e.g. including microseconds), you could use attrgetter from the operator module to get the attributes from the datetime object.

from datetime import datetime
from operator import attrgetter

attrs = ('year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond')

d = datetime.now()
# datetime.datetime(2020, 5, 14, 12, 49, 35, 33067)

d_tuple = attrgetter(*attrs)(d)
# (2020, 5, 14, 12, 49, 35, 33067)

Otherwise, just use the timetuple() as shown in the other answers (probably more efficient if you can live without microseconds).

Converting datetime value in a Tuple to Y-M-D

Does this work ?

sql = "Select Distinct to_char(Datein,'YYYY-MM-DD') from table Order by datein asc"

How to convert a tuple into a string in python and use it into strptime?

No need to use strptime, you could pass your values to datetime.datetime as they are

X.append(datetime.datetime(*Date[i]).date())

Also your code needs some polishing, variable names should be lower cased, instead of iterating over range you could iterate directly over values of date and val.

date = [[2017, 1, 1], [2017, 1, 1]]
val = [0, 1]

x = []
y = []
for i, v in zip(date, val):
if i[0] == 2017:
x.append(datetime.datetime(*i).date())
y.append(v)

Python Pandas: convert <class 'tuple'> to datetime

You can create new DataFrame with constructor and then apply to_datetime, important are column names year, month and day:

a = pd.DataFrame(df['GregDate'].values.tolist(), columns=['year','month','day'])
print (a)
year month day
0 2000 1 1
1 2000 1 1
2 2000 1 2
3 2000 1 3
4 2000 1 3
5 2000 1 4

df.GregDate = pd.to_datetime(a)
print (df)
Sigma JulianDay GregDate
0 -9.05 2451545.0 2000-01-01
1 -10.99 2451545.0 2000-01-01
2 -8.42 2451546.0 2000-01-02
3 -8.92 2451547.0 2000-01-03
4 -10.79 2451547.0 2000-01-03
5 -9.53 2451548.0 2000-01-04

Tuple to datetime

You can use rename_axis with reset_index and assign for create df1.

Then to_datetime and format by strftime, output is assigned back to index:

df1 = df.rename_axis(['month','day']).reset_index().assign(year=2000)
print (df1)
month day Value year
0 1 2 22 2000
1 1 10 30 2000
2 1 15 5 2000
3 2 8 12 2000
4 2 20 15 2000
5 3 5 20 2000

df.index = pd.to_datetime(df1[['day','month','year']]).dt.strftime('%d-%b')
print (df)
Value
02-Jan 22
10-Jan 30
15-Jan 5
08-Feb 12
20-Feb 15
05-Mar 20

Another solution:

idx = df.index.map(lambda x: '-'.join((str(x[0]), str(x[1]), '2000')))
print (idx)
['1-2-2000' '1-10-2000' '1-15-2000' '2-8-2000' '2-20-2000' '3-5-2000']

df.index = pd.to_datetime(idx).strftime('%d-%b')
print (df)
Value
02-Jan 22
10-Jan 30
15-Jan 5
08-Feb 12
20-Feb 15
05-Mar 20

Convert 'yyyy-mm-dd' string to 3-tuple datetime objects in python 2.7

You don't want datetime objects; you want date objects, which are provided by the date method:

from datetime import datetime

dates = ['2014-01-01', '2014-03-07']
date_objects = [datetime.strptime(date, '%Y-%m-%d').date()
for date in dates]


Related Topics



Leave a reply



Submit